【发布时间】:2011-08-04 17:52:21
【问题描述】:
我的 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}
用户类:
<?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 类中,但该类未正确加载。
提前感谢您的每一个帮助!
【问题讨论】:
标签: zend-framework zend-db zend-db-table