Multiple Database from application.ini in Zend Framework


place something like in your application.ini

[production]

resources.multidb.db1.adapter = "pdo_mysql"
resources.multidb.db1.host = "localhost"
resources.multidb.db1.username = "webuser"
resources.multidb.db1.password = "XXXX"
resources.multidb.db1.dbname = "db1"


resources.multidb.db2.adapter = "pdo_pgsql"
resources.multidb.db2.host = "example.com"
resources.multidb.db2.username = "dba"
resources.multidb.db2.password = "notthatpublic"
resources.multidb.db2.dbname = "db2"
resources.multidb.db2.default = true

you should declare the default database as we did in
resources.multidb.db2.default = true

if you want to retrieve a specific database adapter may use from followings:


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

$db1 = $resource->getDb('db1');

$db2 = $resource->getDb('db2');

$defaultDb = $resource->getDb();
//or
$defaultDb = $resource->getDefaultDb();

getDb() returns an instance of a class that extends Zend_Db_Adapter_Abstract.
If you have not set a default database, an exception will be thrown when this method is called without specifying a parameter.

code says it all🙂

 

One thought on “Multiple Database from application.ini in 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