"Dynamic" db

What I’m trying to achieve is the following:

  • There is no guest access to the application, login is required

  • On the login form, the user has to choose a database identifier (dbid)

  • During authentication, a connection string is generated from the dbid and is used together with the username and password to try to connect to the database.

  • Authentication is actually performed by the DB server. If the connection is successful, the connection string, username and password are saved in the user’s session.

  • For subsequent requests, these saved values are used to connect to the database.

Any advice? Could I still use the standard CApplication->db component? When would be the right time (place) to retrieve the connection details and initialize the connection?

you must first create a db component and use the config provided by your user’s form or session

not sure if you can replace the db default component without turn on the connection, I think you cant

Thanks!

When would be the proper moment to do this? Would I need a custom Application (init() or preinit())? Or use the onBeginRequest event?

the onBeginRequest would do it perfectly

Thanks for the tips. And I am actually able to use the ‘default’ db component like this:




	$dbconfig = array(

			'class'=>'CDbConnection',

			'connectionString'=>$session['dsn'],

			'username'=>$session['username'],

			'password'=>$session['password'],

	);


	app()->setComponent('db',Yii::CreateComponent($dbconfig));

}

(This is in the onBeginRequest event)

:rolleyes: helpful code snippet

Hi,

I need something same…

my connection details are in another database…