Zend表行抽象模型方法引发无法识别的方法\'updateFromArray()\'
||
我的Zend Framework应用程序有一个非常具体的问题。我使用我自己的称为My_Model的Model类,该类为单例,并且用于调用数据库模型。每个模型都扩展My_Db_Table类,并具有扩展My_Db_Table_Row的行类。当我调用模型时,可以,但是该模型似乎忽略了行类模型。有人可以告诉我我在做什么错吗?
自定义类:
class My_Model {
protected static $_instance = null;
protected $_services = array();
private function __construct() {
}
/**
* Service classed is called by it\'s name
*
* @param string $name
* @return My_Db_Table
*/
public function getService($name) {
if (!isset($this->_services[$name])) {
$service = new $name();
if (!$service instanceof Zend_Db_Table_Abstract) {
$type = gettype($service);
if ($type == \'object\') {
$type = get_class($service);
}
throw new Zend_Db_Table_Row_Exception(\"Class must be a Zend_Db_Table_Abstract, but it is $type\");
}
$this->_services[$name] = $service;
}
return $this->_services[$name];
}
}
Zend_Db_Table类是:
<?php
/**
* This class represents a DB table
*
*/
class My_Db_Table extends Zend_Db_Table_Abstract {
/**
* Fetches the table row by primary key
*
* @param string $id
* @return Product
*/
public function getById($id) {
return $this->find($id)->current();
}
}
行类:
<?php
/**
* Row class
*
*/
class My_Db_Table_Row extends Zend_Db_Table_Row_Abstract {
/**
* Inflector to get the attribute name
* camelCase -> under_score
*
* @param string $columnName
* @return string
*/
protected function _transformColumn($columnName) {
$inflector = new Zend_Filter_Inflector ( \":string\" );
$inflector->setRules ( array (
\':string\' => array (\'Word_CamelCaseToUnderscore\', \'StringToLower\' ) ) )
;
$columnName = $inflector->filter ( array (\'string\' => $columnName ) );
return $columnName;
}
/**
* Magic method hook to catch getters and setters
*
* @param string $method
* @param array $args
*/
public function __call($method, array $args) {
$matches = array ();
if (preg_match ( \'/^get(\\w+?)$/\', $method, $matches )) {
$attribute = $matches [1];
return $this->{$attribute};
}
if (preg_match ( \'/^set(\\w+?)$/\', $method, $matches )) {
$attribute = $matches [1];
$this->{$attribute} = (count ( $args ) == 1) ? $args [0] : null;
return;
}
return parent::__call ( $method, $args );
}
}
要使用这些类,我使用以下代码:
$record = My_Model::getInstance ()->getService ( \'Users\' )->getById ( 1 );
$record->updateFromArray(array(\'auth_token\' => \'asfsgfgswg\'));
我收到以下错误消息:
Message: Unrecognized method \'updateFromArray()\'
Stack trace:
#0 /home/www/fbdrives/application/modules/facebook/controllers/IndexController.php(34): Zend_Db_Table_Row_Abstract->__call(\'updateFromArray\', Array)
#1 /home/www/fbdrives/application/modules/facebook/controllers/IndexController.php(34): Zend_Db_Table_Row->updateFromArray(Array)
#2 /home/www/fbdrives/library/Zend/Controller/Action.php(513): Facebook_IndexController->indexAction()
#3 /home/www/fbdrives/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch(\'indexAction\')
#4 /home/www/fbdrives/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#5 /home/www/fbdrives/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#6 /home/www/fbdrives/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#7 /home/www/fbdrives/public/index.php(27): Zend_Application->run()
#8 {main}
Users类:
<?php
/**
* User table model class
*
*/
class Users extends My_Db_Table {
/**
* The DB table name
*
* @var string
*/
protected $_name = \'user\';
/**
* The row class name
*
* @var string
*/
protected $_rowclass = \'User\';
}
未使用updateFromArray方法调用的User类:
<?php
/**
* The user model class
*
*/
class User extends My_Db_Table_Row {
/**
* Update values by array values
*
* @param array $values
*/
public function updateFromArray(array $values) {
$this->setFromArray($values);
$this->save();
return $this;
}
}
如果需要,我将提供任何必要的信息。我真的被这个问题困扰了几天,似乎无济于事。方法updateFromArray在User类中,但是该类未正确加载。
在此先感谢您的帮助!
没有找到相关结果
已邀请:
1 个回复
目浆搽
这就是为什么在堆栈跟踪中可以看到Zend_Db_Table被调用的原因,这是未设置为使用自定义类的行的默认类。