Created
September 28, 2012 16:43
-
-
Save donamkhanh/3800862 to your computer and use it in GitHub Desktop.
Set up multiple database in Zend Framework (v1.12)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Setup multiple DB in application.ini like this: | |
; Database 1 | |
resources.multidb.db1.adapter = pdo_mysql | |
resources.multidb.db1.host = 127.0.0.1 | |
resources.multidb.db1.username = root | |
resources.multidb.db1.password = vertrigo | |
resources.multidb.db1.dbname = my_db_1 | |
resources.multidb.db1.default = true | |
; Database 2 | |
resources.multidb.db2.adapter = pdo_mysql | |
resources.multidb.db2.host = 127.0.0.1 | |
resources.multidb.db2.username = root | |
resources.multidb.db2.password = | |
resources.multidb.db2.dbname = my_db_2 | |
resources.multidb.db2.port = 33066 | |
resources.multidb.db2.charset = utf8 | |
2) Add the method bellow to Bootstrap.php | |
$this->bootstrap('multidb'); | |
$multidb = $this->getPluginResource('multidb'); | |
$db1 = $multidb->getDb('db1'); | |
$db1->setFetchMode(Zend_Db::FETCH_OBJ); | |
Zend_Registry::set('db', $db1); | |
$db2 = $multidb->getDb('db2'); | |
$db2->setFetchMode(Zend_Db::FETCH_OBJ); | |
Zend_Registry::set('db2', $db2); | |
3) Using difference db adapter in Zend_Db_Table_Abstract | |
3.1) Setup by __construct method: public function __construct(array('db' => 'db2')); | |
3.2) Setup by _setAdapter() method: $this->_setAdapter('db2'); | |
3.3) Setup by setOptions() method: $this->setOptions(array('db' => $this->_adapter)); | |
3.4) Setup by setDefaultAdapter() method: self::setDefaultAdapter(Zend_Registry::get('db2')); | |
3.5) Set value for $this->_db property: $this->_db = Zend_Registry::get('db2'); | |
All done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment