我使用经典连接,它通过application.ini文件中的读取参数在bootstap中创建。我想使用更多的外部数据库,但我无法从application.ini中读取它们。我更喜欢从主DB读取参数(它是外部数据库,取决于网站)。那么如何有效地在模型中建立连接?现在我每次需要使用它时都会设置该连接。而当我需要使用主DB时,是否需要再连接。这是非常无效的解决方案。
function joinClientDB($id)
{
$web = $this->getById($id);
$dbSettings = array();
$dbSettings['host'] = $web['web_dbHost'];
$dbSettings['username'] = $web['web_dbUsername'];
$dbSettings['password'] = $web['web_dbPassword'];
$dbSettings['dbname'] = $web['web_dbName'];
$this->_db = Zend_Db::factory('pdo_mysql', $dbSettings);
$this->_db->query('SET CHARACTER SET ' . $web['web_dbCharset']);
}
function joinDefaultDb()
{
$this->_db = Zend_Registry::get('db');
}
有人为我提供简单的解决方案吗?
答案 0 :(得分:0)
我建议使用Zend MULTI DB插件。添加到您的applications.ini所有数据库连接:
; Database ONE
resources.multidb.test.adapter =
resources.multidb.test.dbname =
resources.multidb.test.username =
resources.multidb.test.password = ""
resources.multidb.test.host = "your.local.host"
resources.multidb.test.default = true
; Database TWO
resources.multidb.live.adapter =
resources.multidb.live.dbname =
resources.multidb.live.username =
resources.multidb.live.password = ""
resources.multidb.live.host = "your.live.host.com"
现在你可以再将它们放入你的注册表中(bootstrap.php):
protected function _initDbAdapters()
{
$this->bootstrap('multidb');
$resource = $this->getPluginResource('multidb');
$resource>init();
$testDB = $resource->getDb('test');
$liveDB = $resource->getDb('live');
Zend_Registry::set('DB_TEST', $testDB);
Zend_Registry::set('DB_LIVE', $liveDB);
}