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

cli*_*ime 3 bootstrapping zend-framework model exception

这是我在Bootstrap中的代码:

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

这是我得到的一个例外:

( ! ) 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
Run Code Online (Sandbox Code Playgroud)

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

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

dre*_*010 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());
}
Run Code Online (Sandbox Code Playgroud)

  • +1!更多信息 - [Zend应用 - 操作理论 - 依赖性跟踪](http://framework.zend.com/manual/en/zend.application.theory-of-operation.html#zend.application.theory-of- operation.bootstrap.dependency跟踪) (2认同)