Zend:从Bootstrap访问模型会引发异常

时间:2012-02-08 21:52:50

标签: exception zend-framework model bootstrapping

这是我在Bootstrap中的代码:

public function _initRegistry()
{
    $systemConfigModel = new Application_Model_DbTable_SystemConfig();
    Zend_Registry::set('config', $systemConfigModel->getSystemConfig());
}

这是我得到的一个例外:

( ! ) Fatal error: Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for Application_Model_DbTable_SystemConfig' in /usr/share/php5/Zend/Db/Table/Abstract.php on line 755
( ! ) Zend_Db_Table_Exception: No adapter found for Application_Model_DbTable_SystemConfig in /usr/share/php5/Zend/Db/Table/Abstract.php on line 755

如果我在BaseController内调用它,它就可以了。看起来我在application.ini中指定的PDO适配器在Bootstrap执行时尚未初始化(奇怪?)。我该怎么做才能使代码在Bootstrap中运行?是否有必要使用Zend_Db_Table :: setDefaultAdapter()创建和设置适配器;?

我在问,因为如果代码不在Bootstrap中,它需要在两个不同的地方重复,它看起来也像是属于Bootstrap。

1 个答案:

答案 0 :(得分:9)

您是正确的,在引导期间,您的数据库的Zend应用程序资源尚未初始化。

尝试按如下方式更改引导方法,以便显式引导db资源。

public function _initRegistry()
{
    $this->bootstrap('db'); // Bootstrap the db resource from configuration

    $db = $this->getResource('db'); // get the db object here, if necessary

    // now that you have initialized the db resource, you can use your dbtable object
    $systemConfigModel = new Application_Model_DbTable_SystemConfig();
    Zend_Registry::set('config', $systemConfigModel->getSystemConfig());
}
相关问题