How to switch between databases in CakePHP on the fly
Ever needed to connect to another database in your cake app but got stuck and came with another solution? There is a rather simple way to do this and I think it can be extended to other data sources too (not tested but let me know if you do
).
Let’s get to it.
First you need to add the details of all the databases you want to connect to from within CakePHP. To do this you must go to the database.php file and add the login details for each database. You may end up with something similar to the following code:
class DATABASE_CONFIG { var $default = array( 'driver' => 'mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'db1', 'prefix' => '' ); var $website2 = array( 'driver' => 'mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'db2', 'prefix' => '' ); }
Now that we have our database information defined, it’s time toad some code to our AppModel class. Just put the following code in your app_model.php file:
function changeDataSource($newSource) { parent::setDataSource($newSource); parent::__construct(); }
In the app_controller.php we must have the following code also:
function changeDbSource($database = 'default') { $db = ConnectionManager::getInstance(); $connected = $db->getDataSource($database); if($connected->isConnected()) return true; return false; }
Now we are ready to juggle with our databases as we see fit. Let’s say we want to register a user in multiple applications but we only want one form. We might do something like this in the users_controller.php in the add method:
$user = $this->data; // set data source for the website1 $this->User->changeDataSource('website2'); if($this->changeDbSource('website2')) { $this->User->create($user); $this->User->save($user); } // change data source to default $this->User->changeDataSource('default'); if($this->changeDbSource('default')) { $this->User->create($user); $this->User->save($user); }
Hope you find this useful. Happy baking.
Related posts
![[del.icio.us]](http://insanityville.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://insanityville.com/wp-content/plugins/bookmarkify/digg.png)
![[diigo]](http://insanityville.com/wp-content/plugins/bookmarkify/diigo.png)
![[Facebook]](http://insanityville.com/wp-content/plugins/bookmarkify/facebook.png)
![[Friendsite]](http://insanityville.com/wp-content/plugins/bookmarkify/friendsite.png)
![[LinkedIn]](http://insanityville.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Reddit]](http://insanityville.com/wp-content/plugins/bookmarkify/reddit.png)
![[Simpy]](http://insanityville.com/wp-content/plugins/bookmarkify/simpy.png)
![[Slashdot]](http://insanityville.com/wp-content/plugins/bookmarkify/slashdot.png)
![[StumbleUpon]](http://insanityville.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Twitter]](http://insanityville.com/wp-content/plugins/bookmarkify/twitter.png)
![[Email]](http://insanityville.com/wp-content/plugins/bookmarkify/email.png)



4 Responses
to “How to switch between databases in CakePHP on the fly”





OMG, thank you so much for this, I’ve been searching for days. You’d be surprised how uncommon this answer is. Or maybe not if you had to search as long as I did before you solved this on your own.
I’m glad I could help.
Thanks a lot for this! Really helpful.
I only have one question. In my app a user logs in and the app selects the appropriate database (here is where your example comes into action), but: Do changeDataSource and changeDbSource have to be called in every controller of the application?
Thanks a lot for sharing.
Cheers.
No necessarily. You only need to place them where you need them. I placed mine in the app_controller and app_model so that I can use them wherever I need.