1

I am interested in loading some system params into the Yii::app()->params array from the database using a CActiveRecord extension called SiteSetting.

Unfortunately I couldn't find much advice online for this, but believe I can place a method in SiteSetting called loadSiteSettingsToAppParams and add the setting...

'onBeginRequest'=>array('SiteSetting', 'loadSiteSettingsToAppParams')

...to the config.

I would like to know if I can only add this onBeginRequest to the Yii::app() somewhere within the SiteSetting class (to keep my code modular) and whether this is a sensible approach.

Thanks in advance.

Arth
  • 12,789
  • 5
  • 37
  • 69
  • 1
    You can lookup an answer to your question " http://stackoverflow.com/questions/7410045/how-to-use-events-in-yii " – Oladapo Omonayajo Sep 30 '13 at 14:12
  • Thanks, but I read that article prior to posting and it doesn't actually answer my question. Good link though. – Arth Sep 30 '13 at 14:40

2 Answers2

2

Just re-read your question now and I'd try to provide answers.

To the question "I would like to know if I can only add this onBeginRequest to the Yii::app() somewhere within the SiteSetting class (to keep my code modular)": the answer is, You're not restricted to just a Class. You could (theoretically) place it anywhere within your application and also in the config.php file.

As to whether it's a sensible approach, it depends on the time it would take to request those settings from the database and whether you're prepared to add that time to your HttpRequest response time. The onBeforeRequest is fired before every HttpRequest and if the loadSiteSettingsToAppParams method consumes lots of time, you're adding that time to your HttpRequest response time.

I'd advise that you fetch those settings once after login and then update them only when they change (the settings are updated). This way, you could place the call to loadSiteSettingsToAppParams in the UserIdentity class and call it after a successful login.

That's just how I'd go about doing this though.

Hope I helped.

1

The easy & nice way to accomplish this by using a comoponent like SettingComoponent and place in the components directory protected/components then pre load this component in the preload section like this preload => array('log', 'setting', ...). That's it and now you can call this component anywhere you want like Yii:app()->setting->whatever.

I hope this is answer can be useful for you.

Shifrin
  • 154
  • 1
  • 11