How can I switch the database for the entire project like this example:
User 1 uses database test1
User 2 uses database test2
User 3 uses database test3
Like this above... Those ID from User, will come from the default database ( Login page )
PS: Sorry for my bad english, I need some more information, please tell me.
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.