2012-07-15 39 views

回答

1

假設你的數據庫適配器安裝在你的application.ini類似於:

resources.db.adapter = "pdo_Mysql" 
resources.db.params.username = "music" 
resources.db.params.password = "music" 
resources.db.params.dbname = "music"  
resources.db.isDefaultTableAdapter = true // add this line to make this adapter default, important if using more then one adapter. 

在bootstrap.php中你可以創建類似的新方法:

protected function _initDbAdapter() {//can be named anything as long as it starts with _init 
     $db = new Zend_Config($this->getOptions('db'));//get all db adapter resources 
     Zend_Registry::set('DbAdapter', $db); //save to Zend_registry key DbAdapter 
    } 

ByteNudger是正確的如何使用註冊表:
$authAdapter = new Acai_Auth_Adapter_DbTable($dbAdpater);

但是有一種方法可以避免所有這種引導企業湖。如果您只使用一個數據庫適配器,請嘗試:

//This will pass in the currently default adapter, if you only have one adapter... 
$authAdapter = new Acai_Auth_Adapter_DbTable(Zend_Db::getDefaultAdapter); 

希望這會有所幫助。

+0

很好,謝謝! – Ayrx 2012-07-15 12:39:59

1

您必須定義數據庫適配器,其類別Acai_Auth_Adapter_DbTable應通過在Zend_Registry中存儲數據庫適配器並使用密鑰DbAdapter來使用。要做到這一點,最好的地方就是你的Bootstrap.php

protected function _initDb() 
{ 
    // add code to create a `Zend_Db_Adapter_xxx` 
    Zend_Registry::set('DbAdapter') = $dbAdapter; 
} 

如何建立你Zend Framework: Documentation: Zend_Db_Adapter下找到數據庫適配器。

另一種方式是通過數據庫適配器參數:

// create a database adapter 
$authAdapter = new Acai_Auth_Adapter_DbTable($dbAdpater): 

我建議第一種方式,因爲這樣你可以用Zend_Registry::get('DbAdapter')任何地方訪問你的數據庫適配器在您的應用程序。