2012-02-08 64 views
3

這是我在引導代碼:Zend公司:從引導訪問模式會引發異常

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。

回答

9

在引導期間,您是正確的,您的數據庫的Zend應用程序資源尚未初始化。

請嘗試更改您的引導方法,如下所示,以便明確引導數據庫資源。

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()); 
} 
+2

+1!更多信息在這裏 - [Zend的應用 - 操作理論 - 依賴追蹤](http://framework.zend.com/manual/en/zend.application.theory-of-operation.html#zend.application.theory-of- operation.bootstrap.dependency-tracking) – Phil 2012-02-09 00:10:50

+0

謝謝,它的工作原理。 – clime 2012-02-09 01:20:49