• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Autoload multi databases

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

#2
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...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?
Reply

#3
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/da...cting.html

Reply

#4
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?
Reply

#5
CodeIgniter 2.x
Reply

#6
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.
Reply

#7
(01-15-2015, 08: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.
Reply

#8
(01-15-2015, 08:34 AM)UchihaSV Wrote:
(01-15-2015, 08: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....
Reply

#9
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?
Reply

#10
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
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.