Zend 表行抽象模型方法出现无法识别的方法“updateFromArray()”
我的 Zend Framework 应用程序有非常具体的问题。我使用我自己的名为 My_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 类中,但该类未正确加载。
预先感谢您的每一个帮助!
I have very specific problem with my Zend Framework application. I use my own Model class called My_Model which is singleton and I use it for calling DB models. Every model extends My_Db_Table class and has a row class that extends My_Db_Table_Row. When I call the model it is OK but the model seems to ignore row class model. Could anybody advise me what am I doing wrong?
The custom class:
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];
}
}
The Zend_Db_Table classes are:
<?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();
}
}
Row class:
<?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 );
}
}
To use these classe I use following code:
$record = My_Model::getInstance ()->getService ( 'Users' )->getById ( 1 );
$record->updateFromArray(array('auth_token' => 'asfsgfgswg'));
And I get the following error message:
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}
The Users class:
<?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';
}
The User class that is not called with updateFromArray method:
<?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;
}
}
I will provide any necessary info if needed. I am really stucked on this one for a few days now and nothing seems to help. The method updateFromArray is in the User class but the class is not loaded properly.
Thanks in advance for every help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题可能是您使用的是 $_rowclass 而不是 $_rowClass (带有大写 C)。
如果您改为放置此行,它可能会起作用:
这就是为什么在堆栈跟踪中您可以看到 Zend_Db_Table 被调用,这是尚未设置为使用自定义类的行的默认类。
I think the problem may be that you are using $_rowclass and not $_rowClass (with an uppercase C).
If you put this line instead, it may work:
That's why in the stack trace you can see Zend_Db_Table being called, which is the default class for a a row that hasn't been set to use a custom class.