I need to set up a database connection dynamically, so I can't put the connection parameters into the config file.
I tried to put this in a widget, but it does not work
in the widget, called by the layout, I put (it works in the widget itself):
in the view
I get "Property "CWebApplication.datadb" undefined."
Where I can define once this new property for CWebApplication ?
I have to derive CController ?
Thanks for you reply.
Yes, I already read the Guide.
Maybe I was not clear in my first post.
I'd like to set up once, anywhere to be able to access this object from the controllers, views, models, widgets, …
Because CDbConnection extends from CApplicationComponent, we can also use it as an application component. To do so, configure in a db (or other name) application component in the application configuration as follows,
I can't set up in the config file, because my credentials for the (second) database access can change.
Your code should be fine. However, you need to make sure you are calling Yii::app()->datadb after you call setComponents().
You can also call Yii::app()->setComponents('datadb', $connection) to set the component.
thanks for your reply.
So I have to find the first loaded component, which seems to not be a widget.
I will write a file for setting the connection. I'll include it everywhere I need the database connection.
I extended CActiveRecord, as said here http://www.yiiframew…ic,2347.0.html, and also extend CController overwriting init(), where I define the db connection if not already defined.
As drawn here http://www.yiiframew…ide/basics.mvc, I think that CController is one of the first element loaded (and widget is the last before the layout ).
All seems to work.
Further developments will confirm or not…