編輯:
在問候下面的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');
第一個建議不爲我工作的情況。你能詳細說明如何在Bootstrap中設置它嗎? – Tamlyn 2010-02-03 11:08:40
@Tamlyn:看看我上面的修改。 – prodigitalson 2010-02-03 15:47:04