Zend Framework: Connecting to 2 databases
I was in a situation that I need to connect to 2 different databases.
I started by putting the additional credentials to login into the second database inside my config file: /application/config/app.ini
[production] database.adapter = "PDO_MYSQL" database.params.host = "localhost" database.params.dbname = "Database_A" database.params.username = "root" database.params.password = "1234" psdatabase.adapter = "PDO_MYSQL" psdatabase.params.host = "localhost" psdatabase.params.dbname = "Database_B" psdatabase.params.username = "root" psdatabase.params.password = "1234" [development : production] database.params.dbname = "Database_A" database.params.username = "root" database.params.password = "1234" psdatabase.params.dbname = "Database_B" psdatabase.params.username = "root" psdatabase.params.password = "1234" [testing : production] database.params.dbname = "Database_A" database.params.username = "root" database.params.password = "1234" psdatabase.params.dbname = "Database_B" psdatabase.params.username = "root" psdatabase.params.password = "1234"
This config file now has 2 sets of database login credentials identified by: database and psdatabase…
Now like any other Zend Framework application, we setup the database connection in the bootstrap file. /application/bootstrap.php
//bootstrap code... $dbAdapter = Zend_Db::factory($configuration->database); Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); $registry = Zend_Registry::getInstance(); $registry->configuration = $configuration; $registry->dbAdapter = $dbAdapter; $psdbAdapter = Zend_Db::factory($configuration->psdatabase); $registry->psdbAdapter = $psdbAdapter; //bootstrap code
What we now have 2 database connections stored in the registry. dbAdapter, which is set as the default adapter and psdbAdapter, which we will be calling everytime we need to call manually when we need to use it.
For example, I have these models.
<?php /** * Psdump.php * /opt/apache2/htdocs/apps/org_v2/application/models/Psdump.php */ require_once APPLICATION_PATH.'/models/PsdumpTable.php'; class Psdump extends Zend_Db_Table { protected $_table; public function getTable() { if(null === $this->_table) { $this->_table = new PsdumpTable; } return $this->_table; } public function fetchAllEmployees() { //return $this->getTable()->fetchActiveEmployees(); return $this->getTable()->getLatestPSDumpTable(); } }
And this…
<?php /** * PsdumpTable * /opt/apache2/htdocs/apps/org_v2/application/models/PsdumpTable.php * */ class PsdumpTable extends Zend_Db_Table_Abstract { protected $_name = 'psdump'; protected $_psdb; public function init() { //load the other adapter $this->_psdb = Zend_Registry::getInstance('psdbAdapter')->psdbAdapter; } public function insert(array $data) { return parent::insert($data); } /** * fetch all employees with Status = A * * @return unknown */ public function fetchActiveEmployees() { $sql = "SELECT * FROM ".$this->_name." WHERE Status=".$this->_db->quote('A'); return $this->_db->fetchAll($sql); } /** * We are using the psdb here!!! * * @return unknown */ public function getLatestPSDumpTable() { $sql = "SHOW tables"; return $this->_psdb->fetchAll($sql); } }
Browse Timeline
- « Extending the Models in Zend Framework = Better readability + Easier to maintain
- » jQuery: children() VS. find() – which is faster?
Comments ( 3 )
Links for 2009-04…
TODO list 有Firefox扩展,便于管理自己的TODO
一游游戏网 在线Flash游戏。用来消磨时间最好了。
使用Mysql来搭建可扩展的SNS网站(浅谈)谈论了关于MySQL分表的方法。尾部的几个连接相当有用
…
Yet another post intended for myself :P Comments/suggestions — just leave a reply.


