Does someone knows the best practice for using 2 different database in my application?
I need to store data in both databases which are differently located (host,username,password , all does change).
I'm planning to create models as usual, and in construct set the db host,name,pass etc.
Tried this, and it works:
I read that
must be false for these things to work. Please correct me if I'm wrong. For ALL multiple databases you use, it has to be off. I don't know if this is a good solution.
I'm not sure if you call this "best" way, but a way, as described in the tutorial, is this,
in the database file, you have the default configuration, a part of which is:
now you can create another group, say we call it group1 and we want it to have everything the same as the default database settings except for the name, so you can do
then, when you want to use the second database, just go
and then, instead of
$this->db->foo()
, you will do$DB2->foo()
alternatively (as suggested in comments by sbaaaang), you can do
$this->db=$DB2;
to keep everything the sameand you can extend this to multiple groups like this
Well i would like just to write here my solution cause i've used less code i think:
in database.php i set the database groups so for ex:
then in models i used the constructor to switch to database i want to use like this:
if anyone would like to show more solutions do that please! These are just my two cents.