Create multiple database configurations in database.php
Let say we have default, test1, test2, test3 configurations.
As you said user data is in default db config and I consider that you are using AUTH COMPONENT for authorization.
Now add following function in AppController
public function beforeFilter() {
//call change db config function on user login and pass which config need to use
if( $this->Auth->loggedIn() ) {
//user login
///Some logic to select db config according login user.
$dbConfig = "result of logic which return db config name";
$this->_userSpecificDbConfig($dbConfig);
}
}
private function _userSpecificDbConfig($dbConfig) {
//get model name
$model = $this->name;
//set db configuration for user
switch($dbConfig) {
case 'test1':
$this->{$model}->useDbConfig = "test1";
break;
case 'test2':
$this->{$model}->useDbConfig = "test2";
break;
case 'test3':
$this->{$model}->useDbConfig = "test3";
break;
default:
$this->{$model}->useDbConfig = "default";
}
}
Try this code.