I have very specific problem with my Zend Framework applic开发者_开发知识库ation. 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!
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:
protected $_rowClass = 'User';
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.
精彩评论