Zend表行抽象模型方法无法识别的方法'updateFromArray()'

时间:2011-04-13 13:37:07

标签: zend-framework zend-db zend-db-table

我的Zend Framework应用程序存在非常具体的问题。我使用我自己的名为My_Model的Model类,它是单例,我用它来调用DB模型。每个模型都扩展了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 );

}

}

要使用这些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';

}

未使用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 个答案:

答案 0 :(得分:1)

我认为问题可能是您使用$ _rowclass而不是$ _rowClass(使用大写C)。

如果你改用这一行,它可能会起作用:

protected $_rowClass = 'User';

这就是为什么在堆栈跟踪中你可以看到Zend_Db_Table被调用,这是一个尚未设置为使用自定义类的行的默认类。