Change 'db' Component Schema Or Connectionstring Dynamically

Is it possible to change the ‘db’ component’s connectionString or its schema, dynamically.

I am trying to build an application in which the user login data (and some more general stuff) is stored in a common database, whereas the user-specific details are stored in another database unique to the user.

I want to access the common database when the user is logging, and if successful, switch the database in the ‘db’ component to the user specific database and perform further tasks based on the user’s custom settings.

Is it possible to achieve this without using two db connections (as mentioned in the link Multiple-database support in Yii).


Maybe you can achieve that by creating your own component (saying MyDbConnection) wich extends CdbConnection.

Otherwise: Saying that


			'connectionString' => 'pgsql:host=localhost;port=5432;dbname=myDb',

			'emulatePrepare' => true,

			'username' => 'myuserName',

			'password' => 'myPassword',

			'charset' => 'utf8',


'userDb'=>require(dirname(__FILE__).'/userDb.php', //that one will define user database

Holds the login credentials of each user. You can imagine that this db hods also the users personnal db parameters (UserDbName, userLogin, userPassword, maybe encrypted them would be a good idea)

Then in protected/config/userDb.php:


  $userDbName = ... //fetch it from db

  $userLogin = ...  //same

  $userPassword =    //

  return array(

    'class' => 'system.db.CDbConnection',


    'emulatePrepare' => true,

    'username' => $userLogin,

    //and so on 



else throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));