2010-02-01 35 views
1

我想讓我的一個Zend Framework模塊使用與其他數據庫不同的數據庫。 The Manual suggests您可以在application.ini前加上模塊名稱的資源來實現此目的,但我無法使其工作。Zend Framework中模塊特定的數據庫配置

application.ini相關位是:

 
resources.modules[] = "" 

resources.db.adapter = "PDO_MYSQL" 
resources.db.params.host = "localhost" 
resources.db.params.dbname = "maindb" 
resources.db.params.username = "dbuser" 
resources.db.params.password = "..." 
resources.db.params.charset = "utf8" 

terms.resources.db.params.dbname = "termsdb" 

其中terms是模塊的名稱。

有什麼特別的我需要在Bootstrap中進行這項工作?

回答

0

編輯:

在問候下面的OP的第一條評論...

不,我不能上建立一個模塊化DB資源闡述。我所提供的應該在理論上工作,我相信。如果它不知道什麼需要在Zend_Application_Resource_Db的擴展中修改,因爲我躲到我不使用該資源。我有我自己的自定義資源,允許多個數據庫並基於唯一的連接名稱獲取這些數據庫連接。我就可以在:-)

然而ellaborate所以我有一個MyLib_Db類,它擴展Zend_Db它看起來是這樣的:

class MyLib_Db extends Zend_Db 
{ 
    protected $_instance = null; 
    protected $_connections = array(); 

    /** 
    * Standard Zend Framework unified constructor 
    * @param null|array An array of options that will be passed to setOptions 
    */ 
    public function __construct($options = null) 
    { 
    } 

    /** 
    * Standard Zend Framework setOptions implementation 
    * @param array $options and array of options from config 
    * @return MyLib_Db 
    */ 
    public function setOptions(array $options) 
    { 
    } 

    /** 
    * Set the class instance 
    * @param MyLib_Db 
    * @return MyLib_Db 
    */ 
    public static function setInstance($instance) 
    { 
    } 

    /** 
    * Get/create the singleton instance 
    * @return MyLib_Db 
    */ 
    public static function getInstance() 
    { 
    } 

    /** 
    * Get a Zend_Db adapter Instance by unique name 
    * Searches self::$_connections for the $name param as an array key 
    * @param String $name unique connection name 
    * @return Zend_Db_Adpater_Abstract|null 
    */ 
    public function getConnection($connectionName) 
    { 
    } 

    /** 
    * Add a connection instance to the registry 
    * Adds/creates an Zend_Db_Adapter instance to the connection registry with 
    * the string key provided by $name. If $connection is an array|Zend_Config it 
    * should match the format used by Zend_Db::factory as it will be passed to this 
    * function. If $name is null then the database name will be used. 
    * @param Zend_Db_Adapter_Abstract|Zend_Config|array The connection to register 
    * @param string|null $name A unique name for the connection 
    * @return MyLib_Db 
    */ 
    public function addConnection(Zend_Db_Adapter_Abstract $connection, $name = null) 
    { 
    } 

    /** 
    * Remove/Destroy the specified connection from the registry 
    * @param string $name the connection name to remove 
    * @return MyLib_Db 
    */ 
    public function removeConnection($name) 
    { 
    } 
} 

所以基本上爲DB我的應用程序資源創建並返回前述實例類。在創建過程中,它會創建我在配置中設置的任何適配器,並在名稱中註冊它們(它也會查找用於Zend_Db_Table以及執行其他操作的默認標誌)。

然後,我或者使用MyLib_Db::getInstance()->getConnection($name); 或者我從引導中獲得MyLib_Db實例,然後調用getConnection

我個人更喜歡這樣做,因爲它不依賴於應用程序範圍廣泛的連接或綁定到允許更靈活重用的特定模塊。這就是說我實際上只在一些項目中使用過,因爲在我的大多數Zend項目中,我一直使用Doctrine而不是Zend_Db

希望幫助:-)


其實我覺得你需要把在你的模塊一節中的配置類似modules.terms.resources.db.*。這應該使它加載到模塊引導程序中。或者,您可以使用Terms_Bootstrap中的_initDb進行manullay設置。

個人而言,雖然我使用特殊的數據庫管理類,並把它放在我的資源,而不是 - 它處理設置一個或多個適配器。然後在下面的文章中指出$db是從資源數組中檢索到的管理器...

$dbAdapter = $db->getConnection('terms');

+0

第一個建議不爲我工作的情況。你能詳細說明如何在Bootstrap中設置它嗎? – Tamlyn 2010-02-03 11:08:40

+0

@Tamlyn:看看我上面的修改。 – prodigitalson 2010-02-03 15:47:04

0

操作如下:

moduleName.host = "localhost" 
moduleName.username = "user" 
moduleName.password = "pass" 
moduleName.dbname = "dbname" 
moduleName.charset = "utf8" 

然後加在你的模塊的bootstrap.php以下任何初始化函數

$params = $GLOBALS['application']->getOption('moduleName'); 
$dbAdapter = Zend_Db::factory('PDO_MYSQL', array(
    'host' => $params['host'], 
    'dbname' => $params['dbname'], 
    'username' => $params['username'], 
    'password' => $params['password'], 
    'charset' => $params['charset'], 
)); 
Zend_Registry::set('moduleSpecific', $dbAdapter); 

然後在你的模塊的model/DbTable/文件夾中創建一個類,並讓你的表分類來擴展ModuleName_Model_DbTable_Tablemain而不是Zend_Db_Table_Abstract,並且你準備好了。

class ModuleName_Model_DbTable_Tablemain extends Zend_Db_Table_Abstract 
{ 
    /** 
    * 
    * wrapper class constructor used to set db adaptor on the fly 
    * @author Krishan 
    */ 
    function __construct(){ 

     parent::__construct($adapter = Zend_Registry::get('moduleSpecific')); 

    } 
} 

讓我知道什麼問題