Monday, October 4, 2010

Multiple DB in cakePHP

In order to dynamically select DB during runtime in cakePHP is try to do it in Controller or in Model.

To do it in model:

class User extends AppModel {
var $name = 'User';
var $useDbConfig = 'general_syst';

function __construct($id = false, $table = null, $ds = null) {
$this->useDbConfig = 'db1';

parent::__construct($id, $table, $ds);
}

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


When in controller

class Users extends AppController {

function action1(){
$this->User->useDbConfig = 'db1';

$users = $this->User->find('all');

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

}


Hope this one helps.