Multiple Databases with Zend Framework

application.ini

resources.multidb.db1.adapter        = "PDO_MYSQL"
resources.multidb.db1.isDefaultTableAdapter = true
resources.multidb.db1.host           = "localhost"
resources.multidb.db1.username       = "root"
resources.multidb.db1.password       = "password"
resources.multidb.db1.dbname         = "database1"
 
resources.multidb.db2.adapter        = "PDO_MYSQL"
resources.multidb.db2.isDefaultTableAdapter = false
resources.multidb.db2.host           = "localhost"
resources.multidb.db2.username       = "root"
resources.multidb.db2.password       = "password"
resources.multidb.db2.dbname         = "database2"

bootstrap.php

protected function _initDatabase ()
    {
        $resource = $this->getPluginResource('multidb');
        $resource->init();
 
        Zend_Registry::set('db1', $resource->getDb('db1'));
        Zend_Registry::set('db2', $resource->getDb('db2'));
    }

Use by calling:

$db = Zend_Registry::get('db1');
$result = $db->fetchAll ( "select * from mytable", array ());
Advertisements

One thought on “Multiple Databases with Zend Framework

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s