Overwrite config/main.php values from a DB

Is there a way to overwrite parameters in config/main.php, maybe loaded from a table?

I’ve installed dbparam extension that reads/writes params, now I would like to understand how to implement for my purpose.

For example I would like to overwrite Yii::app()->theme with a value taken from a table using dbparam (Yii::app()->par->myThemeValue ).

Now: I have to load those data after config/main.php loading, but before application beginning, right? How does it work? Any solution to do that?

hello,

that’s a cool idea …

The way I’m currently doing something similar (but not from a DB!), I create a configuration file (ie. config.php) and include it before the main.php file loads.

In the config.php I have constants holding my desired variables (ie: MYTHEME = theme, MYSQLSTRING = mysql connection string, MYAPPNAME = app name … and so on)

and in the main.php I call these constants - this way, I don’t have to store sensitive information in my main.php file (I mostly use public source control solutions)

–iM

Hi Imehesz, I’ve solved in this way:

  1. create a normal config/main.php configuration file, with



'config'=>array(

                'class' => 'application.extensions.dbparam.param',

                'connectionID' => 'db',

                'paramsTableName' => '{{settings}}',

                ),



to load DbParam extension. See here for ext: http://www.yiiframework.com/extension/dbparam/

After that, I’ve created a MyController.php inside components directory that extends CController. Other controllers, inside “controller” dir will extends MyController. In MyController.php I have init() done in this way:




  public function init(){

        Yii::app()->config->load(); //it loads all data stored in DB, look dbparam for doc

        Yii::app()->theme = Yii::app()->config->theme; // overwrite default "theme" value, stored in config/main.php

        Yii::app()->name = Yii::app()->config->name; //overwrite default "name" application value, stored in config/main.php

     // .... and so on...

                }



Because init() method will loaded first, from "root" controller, Yii::app()->{whatever} will be available for whole application.

It looks ugly, I know, but at moment it’s only way I’ve found to solve my problem. Other suggestions/solutions are welcome, of course :)

Ciao

Danilo

zitter,

I’m trying to set this up with PHP 5.3, but I’m getting all kinds of errors … what’s your PHP version?

–iM

PHP 5.2.6-1+lenny4 with Suhosin-Patch 0.9.6.2 (cli) (built: Nov 22 2009 02:38:03)

Which errors, btw?