Reputation: 10046
I'm currently using only one database with Zend Framework, but now I have to add ONE MORE.
I'm using this code right now:
public static function setupDatabase()
{
$config = self::$registry->configuration;
$db = Zend_Db::factory($config->db->adapter, $config->db->toArray());
$db->query("SET NAMES 'utf8'");
self::$registry->database = $db;
Zend_Db_Table::setDefaultAdapter($db);
}
What code do I need to write in order to use ONE MORE database; and how I will reference it, when I need to make some queries or so..
Upvotes: 6
Views: 9780
Reputation: 20601
This is included in the framework as Zend_Application_Resource_Multidb
.
Upvotes: 21
Reputation: 3329
I think it will help you
In applocation.ini file
resources.multidb.db1.adapter = "pdo_mysql"
resources.multidb.db1.host = "localhost"
resources.multidb.db1.username = "root"
resources.multidb.db1.password = ""
resources.multidb.db1.dbname = "db1"
resources.multidb.db1.charset = "utf8"
resources.multidb.db1.driver_options.1002 = "SET NAMES utf8"
resources.multidb.db1.default = true
resources.multidb.db2.adapter = "pdo_mysql"
resources.multidb.db2.host = "localhost"
resources.multidb.db2.username = "root"
resources.multidb.db2.password = ""
resources.multidb.db2.dbname = "db2"
resources.multidb.db2.charset = "utf8"
resources.multidb.db2.driver_options.1002 = "SET NAMES utf8"
resources.multidb.db2.default = false
in bootstrap file
protected function _initDbAdaptersToRegistry()
{
$this->bootstrap('multidb');
$resource = $this->getPluginResource('multidb');
$Adapter1 = $resource->getDb('db1');
$Adapter2 = $resource->getDb('db2');
Zend_Registry::set('db1', $Adapter1);
Zend_Registry::set('db2',$Adapter2);
}
more description on http://www.tricksofit.com/2013/10/multiple-database-zend-framework
Upvotes: 1
Reputation: 1467
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
Upvotes: 7
Reputation: 8326
In my situation, I have a 'core' database that loads customer info, including the customer's database connection info. The core connection settings are in the application.ini
. All the code below is in my bootstrap.
Loading 'core' connection (not set to default in ini
):
$db_core = $this->getPluginResource('db')->getDbAdapter();
$db_core->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('db_core', $db_core);
After settings are loaded from 'core' database into the registry:
$settings = Zend_Registry::get('settings');
$db = Zend_Db::factory(
$settings->db_adapter,
array(
'host' => $settings->db_host,
'username' => $settings->db_user,
'password' => $settings->db_pass,
'dbname' => $settings->db_name,
)
);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Db_Table::setDefaultAdapter($db);
Upvotes: 2