CodeIgniter Forums
Autoload multi databases - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Autoload multi databases (/showthread.php?tid=772)



Autoload multi databases - UchihaSV - 01-15-2015

$autoload['libraries'] = array('database'); It load only default db.
How i can autoload, two or more databases?


RE: Autoload multi databases - Avenirer - 01-15-2015

It's a bit lengthy to explain, but you can set the configuration for two or more databases: 


PHP Code:
$active_group 'default';
$active_record TRUE
//first database connection parameters
$db['default']['hostname'] = 'localhost';...
//second database connection parameters
$db['anotherdb'] ='anotherdomain';... 

After that, in the models, if you want connection to another database than the default, you load the database that you are interested in:

PHP Code:
$this->load->database('anotherdb'); 

Please, take a look here: http://avenir.ro/codeigniter-connect-two-different-databases/

PS: I don't think you can have multiple connections in the same time. You usually close one and open another. Or maybe I am wrong?


RE: Autoload multi databases - Rufnex - 01-15-2015

The autoload array loads libaries, helpers, models, etc. .. so in your case the database library is loaded and you connected to the db you inserted in /application/config/database.php . If you need more you have to add your second db parameters to this config.
PHP Code:
$db['db1'] = array(...);
$db['db2'] = array(...); 

Now you can connect to both databases and use them:

PHP Code:
$db1 $this->load->database('db1'TRUE); 
$db2 $this->load->database('db2'TRUE); 

For futher details please read http://www.codeigniter.com/userguide3/database/connecting.html


RE: Autoload multi databases - UchihaSV - 01-15-2015

Yes, i alredy use this way.
But if i load two models in one controllers, server has 500 Internal error, because load databases in each one models it's not good idea.

So i want autoload all my databases, and they become visible for all my models.

I search this question in web, and many people have this problem, but i not found simple solution without crutches.
Why CodeIgniter developers not think about this?


RE: Autoload multi databases - UchihaSV - 01-15-2015

CodeIgniter 2.x


RE: Autoload multi databases - Avenirer - 01-15-2015

Then don't load both databases inside a model. A model is only for one table. And a table is usually found in one database, not spread around databases. Also, do your best to use caching, or, who knows, do the optimization inside the databases.


RE: Autoload multi databases - UchihaSV - 01-15-2015

(01-15-2015, 07:12 AM)Avenirer Wrote: Then don't load both databases inside a model. A model is only for one table. And a table is usually found in one database, not spread around databases. Also, do your best to use caching, or, who knows, do the optimization inside the databases.

U not understand, i'm now load one database in one model.
If Controller load one model all is well.
If Controller load two(with same databases in his constructors), server has 500 internal error.
If i autoload one database and Controller load two or more models it's work fine.

And now i want autoload all my databases, than it's can work.


RE: Autoload multi databases - Avenirer - 01-15-2015

(01-15-2015, 07:34 AM)UchihaSV Wrote:
(01-15-2015, 07:12 AM)Avenirer Wrote: Then don't load both databases inside a model. A model is only for one table. And a table is usually found in one database, not spread around databases. Also, do your best to use caching, or, who knows, do the optimization inside the databases.

U not understand, i'm now load one database in one model.
If Controller load one model all is well.
If Controller load two(with same databases in his constructors), server has 500 internal error.
If i autoload one database and Controller load two or more models it's work fine.

And now i want autoload all my databases, than it's can work.

I don't know if this will work but try also to set $db['default']['pconnect'] to FALSE. And, another thing: after the return of data do a $this->anotherdb->close(); (where anotherdb is the database connection...)


That is all I know. Sure hope it helped you....


RE: Autoload multi databases - UchihaSV - 01-15-2015

pconnect alredy false.
If i connect and close conections in each method in models its work.
But it is not rational.

It would be better, if one time autoload all databases with visibility for all models and forgot all problem with databases.
Is it impossible?


RE: Autoload multi databases - Avenirer - 01-15-2015

As I told you, I don't know if you can do this with CI (but I am considering myself a noob regarding CI). Try taking a look here: https://ellislab.com/forums/viewthread/160029/#770145