CodeIgniter Forums
Multiple database connectivity - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Multiple database connectivity (/thread-54139.html)

Multiple database connectivity - El Forum - 08-24-2012

[eluser]Sanjib Kumar Jena[/eluser]
Hi All,

I have integrated two databases in my site. I can able to access multiple database as bellow.

$this->DBOne = $this->load->database('default', TRUE);
$this->DBtwo = $this->load->database('another', TRUE);

But what's my problem is i have to write the above code in almost all of model constructor.
Is there any trick or method to get all the database objects automatically in the model or controller instead of writing the above code. I mean can we autoload the databases and get the database instances which will be available through out our program.

Multiple database connectivity - El Forum - 08-24-2012

I think one way will be to put this in a class constructor and inherit it using extends where ever required.

if you want to go with autloading then you need to add them in application/config/autoload.php file. Refer the CI user guide regarding autoloading.

Multiple database connectivity - El Forum - 08-24-2012

I believe you can only "autoload" one database connection.

Extend CI's core model class with MY_Model and put them in the constructor there. Then all of your other models would extend MY_Model (instead of CI_Model) and have those properties be available to them.

See the Extending Core Classes section