From what I've found here, in order to use different databases in a Zend application, you can follow one of these two possible ways, according to your needs:
- Having same host/user for the two databases
You can specify the database you want to use initializing the $_schema
variable in the model, as follows:
class Customer extends Zend_Db_Table_Abstract
{
protected $_name = 'customer';
protected $_schema = 'db_name';
....
}
- Having different host/user for the two databases
In application.ini
you have to write the configuration for both databases as follows:
resources.multidb.local.adapter = pdo_mysql
resources.multidb.local.host = localhost
resources.multidb.local.username = user
resources.multidb.local.password = ******
resources.multidb.local.dbname = db_name_1
resources.multidb.local.default = true
resources.multidb.remote.adapter = pdo_mysql
resources.multidb.remote.host = remote_host
resources.multidb.remote.username = user
resources.multidb.remote.password = ******
resources.multidb.remote.dbname = db_name_2
resources.multidb.remote.default = false
Adding the _initDbRegistry
block to bootstrap will add the databases to the registry, so you'll be able to access them:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* Add databases to the registry
*
* @return void
*/
public function _initDbRegistry()
{
$this->bootstrap('multidb');
$multidb = $this->getPluginResource('multidb');
Zend_Registry::set('db_local', $multidb->getDb('local')); //db_local is going to be the name of the local adapter
Zend_Registry::set('db_remote', $multidb->getDb('remote')); //db_remote is going to be the name of the remote adapter
}
}
Now you can specify the adapter you want to use for each model, as follows:
class Customer extends Zend_Db_Table_Abstract
{
protected $_name = 'customer';
protected $_schema = 'db_name_1';
protected $_adapter = 'db_local'; //Using the local adapter
....
}
class Product extends Zend_Db_Table_Abstract
{
protected $_name = 'product';
protected $_schema = 'db_name_2';
protected $_adapter = 'db_remote'; //Using the remote adapter
....
}