Yii 1.1: myconfig

This component allows you to manage your website various config items via database or files.

This component allows you to manage your website various config items via database or files. In my case, i created this component because my projects require an area in the admin panel where the clients can set various config items for the website(Website keywords/description | admin theme | website theme | maintenance theme | contact emails | etc) and i also have some classes that needs to have their params set dynamically from the admin panel.

Basically, you can save your custom config to database, you can load custom config items from files|database and you can cache them if you like.

Important Note

You should use my other extension( http://www.yiiframework.com/extension/settings/ ) as it proven to be more reliable and flexible than this one.


Yii 1.1 (tested on 1.1.5)


In your main.php file, make sure that in the components area, you have something like:

            'class' =>  'application.components.MyConfig',

In the archive you will find a README file which explains detailed how to use this class.

However, this is a short introduction in the methods available:

1) public function setItem($key, $value='')
$key can be a string or an associative array ('key'=>'value');
Saves an item in memory during runtime.
2) public function setDbItem($key, $value='')
It saves the item in database, then calls setItem() to make the item available
3) public function getItem($key)
Retrieves an item. Note that the retrieved item can be a string or array or even object so you need to do additional checks yourself.
4) public function deleteItem($key)
$key can be a string or an array of keys to be removed
Remove item
5) public function deleteDbItem($key)
$key can be a string or an array of keys to be removed
Removes items from database, then calls deleteItem()
6) public function deleteItems()
Removes all items
7) public function deleteDbItems()
Removes all the items from database, then calls deleteItems();
8) public function loadDbItems()
Loads the items from database during runtime, and it saves them to cache it it's the case.
Note, that if the 'loadDbItems' property is set to true, the items are loaded at init, so you don't have to do it manually.
9)public function loadConfigFile($fileArray,$keyName=null)
$fileArray can be a string or an associative array('keyName'=>'fileName');
It will load a config file and make it's contents available via getItem('keyName');
The file needs to return an associative array.
10)public function toArray()
Returns all the stored items
11)public function fromConfigToDb($fileName, $fileKey='')
Almost the same behaviour as loadConfigFile() only it saves the items to database and then calls setDbItem();

Have fun using it as for me it proven to be a life saver component. The methods within the class are documented and also are presented in the README file, but if you have any questions, just let me know.

Also, any feedback is highly appreciated.

Total 9 comments

#3938 report it
twisted1919 at 2011/05/23 05:44am
Here you go

I released the component which uses only the database and relies on categories to store the config items.

Link: http://www.yiiframework.com/extension/settings/

#3932 report it
yiqing95 at 2011/05/22 10:54pm
Could you share your category config class

i am interest to your category config ext class. i am developing backend part of my project ,and want to category config to different group ,so .... if the grouped config values can store to different files ,it will be better for performance reason, too big file may reduce performance.

#3733 report it
twisted1919 at 2011/05/02 08:10pm
Not sure

I am not sure if your comment is addressed to me, but in case it is, then i cannot help you as the admin panel is part of a CMS and has allot of extra dependencies.

#3730 report it
Artur Oliveira at 2011/05/02 01:22pm
Admin Panel

Could you share the admin panel that you use to change the configuration item ?

#3727 report it
twisted1919 at 2011/05/02 09:57am

@got 2 doodle I am really glad you find it useful. Regarding your suggestion, i agree that it would be a nice improvement, but this would require a bit more work and code changing in the extension(i am talking here about the setter methods for the items and mainly the way config files are added to database) and the change may not fit everybody needs. I am sure you found a nice way of doing this on your own, so it cannot be too hard to change the extension as you wish. For this extension, i believe this is the final release as i don't really see what to add further. Also, in my cms, i have a similar class that handles the config BUT it only uses the database, no more config files and it supports categories and lazy loading of the categories items, the cache component is required and the values are serialized.

Basically the signature is :

Yii::app()->settings->set($categoryName, $itemName, $itemValue='', $toDatabase=true);//set an item to category

Yii::app()->settings->get($categoryName, $itemName='');//get the item from category

Yii::app()->settings->get($categoryName);//get all items from category

Yii::app()->settings->delete($categoryName, $itemName='');//delete the item

Yii::app()->settings->delete($categoryName);//delete whole category

This has the advantage that in the admin panel, you can choose which categories to be shown for editing. As i said, there's no magic method of importing config files, but you could do smth like:
$items=include('path/to/file');//returns an array() in key=>value pairs
Yii::app()->settings->set($categoryName, $items);

If you are interested on the extension, please let me know, i will post it into the repository.

#3726 report it
got 2 doodle at 2011/05/02 09:26am
Just a thought

This is a great extension, one possible improvement would be to protected configs loaded from a file from being edited in the admin interface. Perhaps have two types of config files example...

            'configFile'=>'admin_emails,lunch_prefs', //stuff that the site owner can edit
            'protectedConfigFile'=>'customerLimits'// stuff that a superAdmin can edit


#3708 report it
got 2 doodle at 2011/04/29 11:21am
Looks good so far

This looks like it will be a real time saver. But just one comment as I get started with this. First run the configuration should be like this (if using a db)

            'class' =>  'application.components.MyConfig',

Otherwise you get an error if 'loadDbItems' is set to true before the table is created, it seems there is no check to skip this if the table does not exist.


#2823 report it
twisted1919 at 2011/02/14 10:30am
Thanks for suggestions

Thanks for your suggestions and positive comment, i am planning to introduce the support of splitting the items into categories but this might take a while and right now i don't have much free time, but i will make my time so that i can update the extension with your changes, which i believe are great .

On the other hand, i believe(not sure, i need to look into documentation) that Yii::app()->getDb()->tablePrefix returns null if it is not set, so we can remove the if() statement from getTableName() method, and just go with: return Yii::app()->getDb()->tablePrefix . $this->_tableName;

Anyway i will update the extension as soon as possible (the update includes the option for getItem() to return a default value if the item requested is not set) :)

#2822 report it
Muaid at 2011/02/14 10:08am

thank you for this good component ..

i made a simple modification which i what to share with you ..

to make this component support Database table prefix (dynamically) you just need to replace:

public function getTableName()
    return $this->_tableName;

with :

public function getTableName()
    if (isset(Yii::app()->getDb()->tablePrefix))
        return Yii::app()->getDb()->tablePrefix . $this->_tableName;
        return $this->_tableName;

this will do the trick and will support table prefix.

other thing in init() function


i just change its order to :


it make sense for me to create the Table before loading DB items.

and again thank you very much for this great ext.

Leave a comment

Please to leave your comment.

Create extension