Yep, i found that i could throw in some more database connections i main.php
And Emil, i am assuming your dbid is comming from the login form, am i right?
This would be an idea but i want to be even more dynamical =P.
I want a user to login and then have a dropdown menu which on selection alters the database connection to the selected db.
as it stands now i can get it to change db but i am unable to decide which db if there is more then one.
Right now the database is changed because my model class overrides getDbConnection() from CActiveRecords which a hardcoded db.
I tried to add a param to getDbConnection() to be able to make a switch but that was a no go due to the overriding function have to be identical.
So my real beef right now is that i some how need to pass along some kinde of paramter that allows me to enter a switch in the overridden getDbConnection() in my model class so that i can controll which db it connects to.
Any suggestions? And thanks for your replies so fare!
Oh and Emil, i looked into the onBeginRequest event.
The dokumentation is less the great for beginners, so where do you place the onBeginRequest method?
That sounds like a good solution if I were to have alot of different user access levels but as it stands now there will only be an admin with access to everything and a user which have access to all DB’s. (My bad for not telling how this part should be)
My problem as it stands now is to be able to form a dropdown menu from the DB connections specified in the main.php. If this is not do able ill just make an array with the names of the DB as key and the login information as value.
What i need after that is a way to alter the connection on an event like onSelectionChanged but im very confused by the events.
Is there such an event or do i need to make it my self? If yes, any help would be appriciated as im having a hard time understanding how events are set up and in which classes.
I think still proposed solution works fine. is it not?
My problem as it stands now is to be able to form a dropdown menu from the DB connections specified in the main.php. If this is not do able ill just make an array with the names of the DB as key and the login information as value.
There is no need to have information in about all dbs in the main.php as you are now storing all the information in one db.
Now, what you have to do is to create an SQL Query which will fetch all the information required to connect to the SQL server and present it in any from, radio buttons or select able list or a linked form.
What i need after that is a way to alter the connection on an event like onSelectionChanged but im very confused by the events.
Try the code as given in one of the reply to this topic as
$dbconfig = array(
'class'=>'CDbConnection',
'connectionString'=>connection information i.e. server,username,password,database,
);
Yii::app()->setComponent('db',Yii::CreateComponent($dbconfig));
Okay, i pasted in the “‘onBeginRequest’=>array(‘Bootstrap’,‘begin’)” in main.php in the config map and made the Bootstrap.php in components.
So what happens now? i filled out the code in begin() with a new connection but when i go to one of model controllers, they get their content from the local server still am i missing something (And truth be told, at this point im getting very very confused )
You setup an event(class Bootstrap method begin) to execute before any other action (onBeginRequest)
in the method you created a "db" component that will be later (CActiveRecord, Models, etc) used by as main db connection ( by default "db" is the main db connection)
A few questions: I still dont really get the event idea. Is this event predefined somewhere or does all events just get called everytime anything happens? Having a hard time seeing what defines when the various events get called (I tried reading up on it, but too abstract writeups imo)
Second, now the begin() metod alters the connection as it should. Is there any way i can pass along the parameters from a user choosed database to begin() to be able to pick which database the user should connect to?
Yes, events are predefined, and for example the event ‘onBeginRequest’ will call the ‘beginRequest’ method by default
And that is why you use “beforeSave” method in your models and “afterSave”, these are actions attached to the ‘onBeforeSave()’ and ‘onAfterSave()’ events
You can attach more actions to be execute when an event occurs
The guy that answered you gave some idea, he uses the session to do it