Yii Framework Forum: "Dynamic" db - Yii Framework Forum

Jump to content

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

"Dynamic" db Selection of db on login Rate Topic: ***** 1 Votes

#1 User is offline   EmilT 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 14-February 11

Posted 22 February 2011 - 04:10 AM

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?
0

#2 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 22 February 2011 - 04:21 AM

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
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#3 User is offline   EmilT 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 14-February 11

Posted 22 February 2011 - 07:14 AM

View PostGustavo, on 22 February 2011 - 04:21 AM, said:

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?
0

#4 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 22 February 2011 - 07:28 AM

the onBeginRequest would do it perfectly
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#5 User is offline   EmilT 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 14-February 11

Posted 23 February 2011 - 11:14 AM

View PostGustavo, on 22 February 2011 - 07:28 AM, said:

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

#6 User is offline   yiqing95 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 603
  • Joined: 27-December 10
  • Location:china

Posted 11 October 2011 - 08:08 AM

View PostEmilT, on 23 February 2011 - 11:14 AM, said:

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
0

#7 User is offline   gaurish 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 05-March 12
  • Location:Pune, India

Posted 21 December 2012 - 12:26 AM

Hi,
I need something same....

my connection details are in another database...

http://stackoverflow...cording-to-user
Thanks,
Gaurish
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