2011-04-13 73 views
0

我有我的Zend Framework應用程序非常具體的問題。我使用我自己的叫做My_Model的Model類,它是singleton,我用它來調用數據庫模型。每個模型都擴展了My_Db_Table類,並有一個擴展My_Db_Table_Row的行類。當我調用模型時,它是可以的,但模型似乎忽略了行級模型。有誰能告訴我我做錯了什麼?Zend錶行抽象模型方法thows無法識別的方法'updateFromArray()'

自定義類:

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(); 

} 

}

Row類:

<?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); 

} 

} 

要使用這些CLASSE我用下面的代碼:

$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'; 

} 

User類不與updateFromArray方法叫:

<?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

我認爲問題可能是您使用$ _rowclass而不是$ _rowClass(使用大寫的C)。

如果你把這個行,而不是,它可能工作:

protected $_rowClass = 'User'; 

這就是爲什麼在堆棧跟蹤你可以看到Zend_Db_Table類被調用,這對於尚未設置到AA行默認類使用自定義類。

+0

你絕對是搖滾!非常感謝你。花了我很多時間來玩這個。這有點複雜,我不是作者,在這些可怕的日子過後,我幾乎失明瞭,錯過了那一天!再次感謝。爲你+1 :-) – Bery 2011-04-13 14:57:04