Zend Framework - Db Adapter在构造函数中为空


Zend Framework - Db Adapter in constructor is null

ZF Version: 1.11.2

我想创建一个抽象类,它包含我的db适配器。简化如下:

<?php
class Application_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
{
    protected $_dbAdapter = null;
    /**
     * @return null
     */
    public function getDbAdapter()
    {
        return $this->_dbAdapter;
    }
    /**
     * @param null $dbAdapter
     */
    public function setDbAdapter($dbAdapter)
    {
        $this->_dbAdapter = $dbAdapter;
    }
    public function __construct()
    {
        'Zend_Debug::dump($this->getAdapter(), 'Datei: ' . __FILE__ . '<br/>Zeile: ' . __LINE__, true); die;
    }
}

但是我的dump返回null。在基本模型类(从Zend_D b_Table_Abstract扩展)中,相同的转储返回一个Zend_Db_Adapter_Pdo_Mysql对象。为什么?

我稍微看了一下Zend DB源代码,只是为了刷新我的记忆。如果你把构造函数改成这样:

public function __construct($config = array())
{
    parent::__construct($config);
    'Zend_Debug::dump($this->getAdapter(), 'Datei: ' . __FILE__ . '<br/>Zeile: ' . __LINE__, true); die;
}

它应该更像你期望的那样工作。

回答您评论中的问题:Zend_Db_Table_AbstractgetAdapter()方法只是返回DB适配器变量中的任何内容。它不会对默认值做任何事情。而且,由于你的类覆盖了那个方法,你的版本将被使用。

如果没有提供的话,Zend_Db_Table_Abstract类中的构造函数将设置一个默认的,所以在我的例子中调用父构造函数应该确保设置一个默认的适配器。

同时,ZF1表示生命已经过了终点。您不应该将其用于新的应用程序,而应该为当前的应用程序规划迁移。