4webby BLOG

11 12 2007

CakePHP models: using multiple DB connections

by Daniel | hits(25826)

TAGS: cakephp multiple DB connections models

 

In this tutorial we will see how you can grab your data from different/multiple DB connections in the same application.

This is particularly usefull if you need to integrate your application with an existing system: let's immagine for example that your application needs to retrieve users data, and that the users data are shared by the general system you are integrating to in a different DB.

Let's get more real. The general application runs on a DB called DB_2, while your CakePHP application runs on DB_1. Users data are in DB_2, while all your app data are in DB_1.

First of all you need to specify 2 (or more DB connections) in app/config/database.php

class DATABASE_CONFIG {

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_host',
'login' => 'your_login_1',
'password' => 'your_password_1',
'database' => 'DB_1',
'prefix' => ''
);

var $general_syst = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_host',
'login' => 'your_login_2',
'password' => 'your_password_2',
'database' => 'DB_2',
'prefix' => ''
);
}
?>

By default CakePHP uses the 'default' connection in your models, if it exists in your database.php configuration.

An important thing is that if you use multiple DB connections in your models you should not set their connection as persistent: namely the 'persistent' key of your dB_connection array should be set to false.

This avoids that your application will use persistently the last connection you switched to in your models.

The key variable that will switch DB config in your models is: $useDbConfig

But let's build our User model and our Post model. They will respectively use:

  • User model: general_syst connection
  • Post model: default connection

User model (in app/models/user.php):

class User extends AppModel {

var $name = 'User';
var $useDbConfig = 'general_syst';

//your code here
//....
}

Post model (in app/models/post.php):

class Post extends AppModel {

var $name = 'Post';
var $useDbConfig = 'default';

//your code here
//....
}

In the Post model it wasn't really necessary to specify the $useDBconfig variable, because we CakePHP uses the 'default' DB config if nothing else is specified, but I specified it just to make the example clearer.

Right! Noe every time you will use in your controllers:

  • $this->Post->bla_bla_bla : data will be retrieved/inserted/updated from DB_1
  • $this->User->bla_bla_bla : data will be retrieved/inserted/updated from DB_2

It's quite easy! Just remember to set your DB configurations as NOT persistent, as I pointed out above!

Happy baking!

Dan

view/hide comments | add comment

  • chirag

    2012-01-31 10:19:56

    hello its true for multiple db connection but
    In belongsTo relation ship it not working
    If you have solution please help me
    thank you

4webby.com

Tags

powered by 4webby.com