Using multiple database in CakePHP 2.2.2
Edit your app/Config/database.php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'username1',
'password' => 'password1',
'database' => 'databasename1',
'prefix' => '',
//'encoding' => 'utf8',
);
public $sms_gateway = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '192.168.1.1',
'login' => 'username2',
'password' => 'password2',
'database' => 'databasename2',
'prefix' => '',
//'encoding' => 'utf8',
);
}
Create a model of the table to use from the second database (databasename2) .
Let's say our table to use is model_table_from_second_dbs .
Create ModelTableFromSecondDB.php inside app/Model/
<?php
App::uses('AppModel', 'Model');
class ModelTableFromSecondDB extends AppModel {
public $useDbConfig = 'databasename2'; //indicate the database to use
}
You can now use ModelTableFromSecondDB model.
Nice tip, I'm using it but what about associations between 2 models from different database? lets say a hasMany association.
ReplyDeleteHow do you do it?
I think this is what your trying to do: http://www.foldifoldi.com/?p=436
ReplyDelete