簡體   English   中英

Zend Framework中特定於模塊的數據庫配置

[英]Module-specific database config in Zend Framework

我希望我的Zend Framework模塊之一使用與其他模塊不同的數據庫。 手冊建議您可以使用模塊名稱為application.ini的資源添加前綴,以實現此目的,但我無法使其正常工作。

我的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中放入一些特殊的東西嗎?

進行如下操作:

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

然后在任何init函數中在模塊的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'));

    }
}

如有任何問題請通知我

編輯:

關於OP在下面的第一條評論...

不,我無法詳細說明如何設置模塊化數據庫資源。 我提供的資料在理論上應該有效。 如果不是,我不確定在Zend_Application_Resource_Db的擴展名中需要修改什么,因為我逃避使用該資源。 我有自己的自定義資源,該資源允許多個數據庫並根據唯一的連接名稱獲取這些數據庫連接。 但是我可以在這方面炫耀:-)

所以我有一個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)
   {
   }
}

因此,基本上,我的數據庫應用程序資源創建並返回了先前類的實例。 在創建過程中,它將創建我在配置中設置的所有適配器,並使用一個名稱將其注冊到該類實例中(它還會查找用於Zend_Db_Table的默認標志以及進行其他操作)。

然后我要么使用MyLib_Db::getInstance()->getConnection($name); 或者我從引導程序中獲取MyLib_Db實例,然后調用getConnection

我個人更喜歡這樣做,因為它不依賴於應用程序范圍內的連接,也不依賴於特定模塊的連接,從而可以更靈活地重用。 那就是說我實際上只在幾個項目中使用了它,因為在我的大多數Zend項目中,我一直在使用Doctrine而不是Zend_Db

希望有幫助:-)


實際上,我認為您需要將其放置在配置的modules.terms.resources.db.*部分中,例如modules.terms.resources.db.* 這應該使其加載到模塊引導程序中。 或者,您可以在自己的Terms_Bootstrap使用_initDb進行Terms_Bootstrap

就我個人而言,盡管我使用特殊的數據庫管理類並將其放在我的資源中-它處理設置單個或多個適配器。 然后在下面假設$db是從資源數組中檢索到的管理器...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM