Janis Peisenieks
Janis Peisenieks

Reputation: 4988

Zend MultiDb resource

I have configured my application to work with multiple databases. The magic works just fine. In my Bootstrap.php I have defined the folowing:

protected function _initDb()
    {
        $resource = $this->getPluginResource('multidb');
        Zend_Registry::set("multidb", $resource);

    }

and in my 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

All of this works just fine. In my models, I usually do something like this in the init():

$multidb = Zend_Registry::get("multidb");
$this->oracle = $multidb->getDb('oracle');

But I recently wanted to move this part to the Bootstrap, or rather, Registry, like this:

protected function _initDb()
    {
        $resource = $this->getPluginResource('multidb');
        Zend_Registry::set("multidb", $resource);
        Zend_Registry::set("odb",$resource->getDb('oracle'));
    }

And this is what happened:

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

I know a temporary fix around this, but why is this happening, and what could be a more long term fix, so that I could set each adapter in registry, preferably in bootstrap? Thanks!

Upvotes: 1

Views: 6403

Answers (1)

Kees Schepers
Kees Schepers

Reputation: 2248

I think you have to make sure the multidb resource is bootstrapped in other to be filled. You can do this with the following:

protected function _initDb() {         
    $this->bootstrap('multidb');

    $resource = $this->getPluginResource('multidb');         

    Zend_Registry::set("multidb", $resource);         
    Zend_Registry::set("odb",$resource->getDb('oracle'));     
} 

Upvotes: 6

Related Questions