zend_db_table setRowClass不绑定我的自定义类而不是Zend_Db_Table_Row

时间:2011-11-11 20:08:26

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

想要少写,我试图扩展默认的Zend ORM系统。我的意思是如果我们通过Zend_Db_Table从Db获得一些数据,我们可以稍后使用Zend_Db_Table_Rowset或Zend_Db_Table_Row类处理这些数据。 我的想法是使用这样的自定义Factory类:

class Go_Factory {

    const PREFIX = 'mule_';

    /**
    * get from database item by specified primary key
    *
    */
    public static function get( $class_name, $identity ){
        return self::getDbTable( $class_name )->find( $identity );
    }

    /**
    * well, there is a corelation between class name and represented by it table name in DB
    * so let's get one from another
    * return instance of Zend_Db_Table with defined _name and _rowclass parameters and 
    */
    public static function getDbTable( $class_name ){
        $db_table_class = str_replace( "Model_", "Model_DbTable_", $class_name ) . "s";
        $row_class = class_exists( $class_name ) ? $class_name : "Core_Model_Item";

        if( !( class_exists( $db_table_class ) ) ){

            $temp = explode( "_", $class_name );
            $table_postfix = strtolower( preg_replace( '/([^A-Z])([A-Z])/', "$1_$2", $temp[ 2 ] ) );
            $table_name = Zend_Registry::get( 'prefix' ) . strtolower( $temp[ 0 ] ) . '_' . $table_postfix . 's';
            $db_table = new Zend_Db_Table( array( 'name' => $table_name ) );
            $db_table->setRowClass( $row_class );
            return $db_table;
        } else {
            return new $db_table_class();
        }
    }

}

想法的核心是,如果请求的class_name已定义,并且未将Core_Model_Item作为实例化Zend_Db_Table类的rowClass,则使我的工厂看起来。当然Core_Model_Item扩展了Zend_Db_Table_Row类。

$db_table->setRowClass( $row_class );行未生效!我已经尝试检查rowClass是否成功定义,只是在定义后立即放置var_dump( $db_table->getRowClass() );并显示"" - 没有设置!最近这给我带来了下一个错误:

Warning: include_once(.php) [function.include-once]: failed to open stream: ��� ������ ����� ��� �������� in /home/users2/n/newpanel/domains/newpanel.jino/library/Zend/Loader.php on line 146

Warning: include_once() [function.include]: Failed opening '.php' for inclusion (include_path='/home/users2/n/newpanel/domains/newpanel.jino/application/../library:/home/users2/n/newpanel/domains/newpanel.jino/library:.:/usr/local/zend/share/pear') in /home/users2/n/newpanel/domains/newpanel.jino/library/Zend/Loader.php on line 146

Fatal error: Uncaught exception 'Zend_Exception' with message 'File ".php" does not exist or class "" was not found in the file' in /home/users2/n/newpanel/domains/newpanel.jino/library/Zend/Loader.php:99 Stack trace: #0 /home/users2/n/newpanel/domains/newpanel.jino/library/Zend/Db/Table/Abstract.php(1357): Zend_Loader::loadClass('') #1 /home/users2/n/newpanel/domains/newpanel.jino/library/Go/Factory.php(30): Zend_Db_Table_Abstract->fetchAll(Object(Zend_Db_Table_Select)) #2 /home/users2/n/newpanel/domains/newpanel.jino/application/modules/core/plugins/Acl.php(22): Go_Factory::reference('User_Model_Role') #3 /home/users2/n/newpanel/domains/newpanel.jino/application/Bootstrap.php(28): Core_Plugin_Acl::getAcl() #4 /home/users2/n/newpanel/domains/newpanel.jino/library/Zend/Application/Bootstrap/BootstrapAbstract.php(667): Bootstrap->_initNavigation() #5 /home/users2/n/newpanel/domains/newpanel.jino/library/Zend/Application/Bootstrap/BootstrapAbstract.php(620): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('na in /home/users2/n/newpanel/domains/newpanel.jino/library/Zend/Loader.php on line 99

请帮我弄清楚我是否做错了。

1 个答案:

答案 0 :(得分:1)

抛出的致命错误是由于将空类发送到Zend_Loader。看起来$db_table_class导致一个空字符串,这意味着$class_name参数也是空的。检查您拨打Go_Factory::get();的位置,并确保您的字符串不为空。

另外,你应该改变这个:

if( !( class_exists( $db_table_class ) ) ){

到此:

if (!class_exists($db_table_class)) {