Yii Framework Forum: Change 'db' Component Schema Or Connectionstring Dynamically - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Change 'db' Component Schema Or Connectionstring Dynamically Rate Topic: -----

#1 User is offline   Mohit 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 06-July 11

Posted 10 December 2012 - 03:20 PM

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).
0

#2 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 10 December 2012 - 05:55 PM

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

Otherwise: Saying that

		'db'=>array(
			'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:

if(!Yii::app()->user->isGuest){
  $userDbName = ... //fetch it from db
  $userLogin = ...  //same
  $userPassword =    //
  return array(
    'class' => 'system.db.CDbConnection',
    'connectionString'=>'mysql:host=localhost;dbname='.$userDbName,
    'emulatePrepare' => true,
    'username' => $userLogin,
    //and so on 
  );
}
else throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));

Hey Ho !
Let's go !
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users