Using two databases in a CakePHP WebApp


Two use two databases in your webapp first you have to configure your database.php which is located under app\config

class DATABASE_CONFIG
{

var $default = array(
‘driver’ => ‘mysql’,
‘persistent’ => ‘false’,
‘host’ => ‘122.0.0.28’,
‘login’ => ‘user1’,
‘password’ => ‘mypassword’,
‘database’ => ‘db1’,
);

var $dbCon2 = array(
‘driver’ => ‘mysql’,
‘persistent’ => ‘false’,
‘host’ => ‘125.0.0.288’,
‘login’ => ‘user2’,
‘password’ => ‘mypassword2’,
‘database’ => ‘db2’,
);

}

In your models if you did not specify the database configuration CakePHP will automatically use $default database setting. Now if your model is going to use your second database configuration you have to specify it inside your models by

var $useDbConfig = ‘dbCon2‘;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: