我已将我的应用程序配置为使用多个数据库。魔术效果很好。在我的Bootstrap.php中,我已经定义了以下内容:
protected function _initDb()
{
$resource = $this->getPluginResource('multidb');
Zend_Registry::set("multidb", $resource);
}
并在我的application.ini中:
resources.multidb.db1.adapter = mysqli
resources.multidb.db1.host = localhost
resources.multidb.db1.username = user
resources.multidb.db1.password = pass
resources.multidb.db1.dbname = db
resources.multidb.db1.charset= "utf8"
resources.multidb.db1.default= true
resources.multidb.db1.profiler.enabled = true
resources.multidb.oracle.adapter = oracle
resources.multidb.oracle.username = user
resources.multidb.oracle.password = pass
resources.multidb.oracle.charset= "utf8"
resources.multidb.oracle.dbname = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(Host = host.example.com) (Port = 1529)) (CONNECT_DATA = (SID = DB)))"
resources.multidb.oracle.profiler.enabled = true
所有这一切都很好。在我的模型中,我通常会在init()
:
$multidb = Zend_Registry::get("multidb");
$this->oracle = $multidb->getDb('oracle');
但是我最近想把这部分移到Bootstrap,或者更确切地说是Registry,就像这样:
protected function _initDb()
{
$resource = $this->getPluginResource('multidb');
Zend_Registry::set("multidb", $resource);
Zend_Registry::set("odb",$resource->getDb('oracle'));
}
这就是发生的事情:
Fatal error: Uncaught exception 'Zend_Application_Resource_Exception' with message 'A DB adapter was tried to retrieve, but was not configured' in C:\Zend\Apache2\htdocs\Zend\Application\Resource\Multidb.php on line 135
我知道一个临时解决方案,但为什么会发生这种情况,哪些可能是一个更长期的修复,以便我可以在注册表中设置每个适配器,最好是在bootstrap? 谢谢!
答案 0 :(得分:6)
我认为您必须确保将multidb资源引导到其他要填充的内容中。您可以使用以下方法执行此操作:
protected function _initDb() {
$this->bootstrap('multidb');
$resource = $this->getPluginResource('multidb');
Zend_Registry::set("multidb", $resource);
Zend_Registry::set("odb",$resource->getDb('oracle'));
}