multiple database connection with Zend framework 2

For programming and general questions on Zend Framework

multiple database connection with Zend framework 2

Postby gaurangrajvir on Tue Jan 08, 2013 2:15 pm

Hello All,

I want to connect two database in one application.

One database from Mysql and another from Sql server.

How can I do this in ZF 2 ?

Has anyone know about this then please suggest.

Thanks in advance.
Gaurang
gaurangrajvir
 
Posts: 4
Joined: Tue Dec 18, 2012 6:01 am

Re: multiple database connection with Zend framework 2

Postby hczo8uk9prrpljya12x on Tue Jan 22, 2013 5:50 am

first setting file application.ini, ex:
resources.multidb.db1.adapter = "mysqli"
resources.multidb.db1.isDefaultTableAdapter = true
resources.multidb.db1.host = "localhost"
resources.multidb.db1.username = "xx"
resources.multidb.db1.password = "xx"
resources.multidb.db1.dbname = "zftutorial"

resources.multidb.db2.adapter = "mysqli"
resources.multidb.db2.isDefaultTableAdapter = false
resources.multidb.db2.host = "localhost"
resources.multidb.db2.username = "xx"
resources.multidb.db2.password = "xx"
resources.multidb.db2.dbname = "bboard"

setting bootstrap.php, ex:
protected function _initDatabase ()
{
$resource = $this->getPluginResource('multidb');
$resource->init();

Zend_Registry::set('db1', $resource->getDb('db1'));
Zend_Registry::set('db2', $resource->getDb('db2'));
}

then you can call by doing like this :
$db = Zend_Registry::get('db1');
$result = $db->fetchAll ( "select * from mytable", array ());

hope it helps
hczo8uk9prrpljya12x
 
Posts: 1
Joined: Tue Jan 22, 2013 5:45 am

Re: multiple database connection with Zend framework 2

Postby saruniiit on Thu Jan 24, 2013 11:31 am

is it working. i am getting error

Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'Configuration array must have a key for 'dbname' that names the database instance' in E:\zend\Apache2\htdocs\twitter\library\Zend\Db\Adapter\Abstract.php:287 Stack trace: #0 E:\zend\Apache2\htdocs\twitter\library\Zend\Db\Adapter\Abstract.php(183): Zend_Db_Adapter_Abstract->_checkRequiredOptions(Array) #1 E:\zend\Apache2\htdocs\twitter\library\Zend\Db.php(270): Zend_Db_Adapter_Abstract->__construct(Array) #2 E:\zend\Apache2\htdocs\twitter\library\Zend\Application\Resource\Multidb.php(99): Zend_Db::factory('PDO_MYSQL', Array) #3 E:\zend\Apache2\htdocs\twitter\application\Bootstrap.php(9): Zend_Application_Resource_Multidb->init() #4 E:\zend\Apache2\htdocs\twitter\library\Zend\Application\Bootstrap\BootstrapAbstract.php(669): Bootstrap->_initDatabase() #5 E:\zend\Apache2\htdocs\twitter\library\Zend\Application\Bootstrap\BootstrapAbstract.php(622): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('database') #6 E:\zend\Apache2\htdocs\twitt in E:\zend\Apache2\htdocs\twitter\library\Zend\Db\Adapter\Abstract.php on line 287
saruniiit
 
Posts: 2
Joined: Thu Jan 24, 2013 11:30 am

Re: multiple database connection with Zend framework 2

Postby saruniiit on Thu Jan 24, 2013 12:53 pm

finally got it.


in the application.ini

dont put params


so it will be like this

resources.multidb.db1.adapter = PDO_MYSQL
resources.multidb.db1.isDefaultTableAdapter = true
resources.multidb.db1.host = "localhost"
resources.multidb.db1.username = "root"
resources.multidb.db1.password = ""
resources.multidb.db1.dbname = ""

resources.multidb.db2.adapter = PDO_MYSQL
resources.multidb.db2.isDefaultTableAdapter = false
resources.multidb.db2.host = "localhost"
resources.multidb.db2.username = "root"
resources.multidb.db2.password = ""
resources.multidb.db2.dbname = ""
saruniiit
 
Posts: 2
Joined: Thu Jan 24, 2013 11:30 am

Re: multiple database connection with Zend framework 2

Postby pragnesh on Fri Feb 01, 2013 5:46 pm

Good article..!!
pragnesh
 
Posts: 1
Joined: Fri Feb 01, 2013 5:44 pm


Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 5 guests