Yii 1.1: config

Get/Set config entries

This extension allows you to store special config entries needed by your application into a database.




  • Yii 1.0 or above


  • Extract the release file under protected/extensions
  • Modify your application config for the use of the extension
   'components' => array(
      'config' => array(
         'class' => 'application.extensions.EConfig',


In order to use this extension, you have to make sure a database component is configured in your config. Also for better performance a configured cache component is a good idea.

These properties are available and can be set in the config:

  • configTableName Defaults to "Config". This is the name of the database table used to store the config entries.
  • autoCreateConfigTable Defaults to true. Wether the database table should be auto-created. For performance reasons you should turn this off once the table got created.
  • connectionID Defaults to "db". The ID of the database component.
  • cacheID Defaults to false. The ID of the cache component. If set to false, no caching will be performed.
  • strictMode Available since version 1.1. Defaults to true. If this is set to false, you can get/set entries even if the entry is not yet present in the database.


By default the created database table is empty. That means you can't get or set any config entry. In order to make it work, you have to add every needed config entry to the database (for example with PHPMyAdmin). Since the extension stores values serialized, you have to make sure you add the needed entries with NULL as value. After that you can safely get or set your entries.

Since version 1.1 you can set the new strictMode property to false in order to get or set a value even if the entry is not present in the database. That works as follows: If you try to get an entry which is not present, NULL is returned. If you try to set an entry which is not present, the entry will be auto-inserted into the database. Please note that if you have a typo in your $key value when getting an entry, NULL is returned instead of a thrown exception. I suggest to just leave strict mode enabled.

To get an entry:

$var = Yii::app()->config->get($key);

To set an entry:

Yii::app()->config->set($key, $value);

Since values are stored serialized, it's no problem to work with booleans or arrays.

Change Log

January 8, 2010

  • Version 1.11: Fixed small bug regarding serialization

January 6, 2010

  • Version 1.1: Added $strictMode property

January 6, 2010

  • Initial release.

Total 6 comments

#10217 report it
CodeButterfly at 2012/10/11 08:01pm
RE: Interesting coding approach! :)

@Trejder it is unusual although I have seen it before. I believe the purpose of this style it to try to eliminate the class of bugs where an assignment is mistakenly used instead of an equal comparison. E.g.:

if($cache == false)
if($cache = false) // oops! accidental assignment

Personally I don't care for it. Of course in addition the boolean keywords are not really necessary with boolean variables (especially if a boolean variable naming convention is used such that variables are prefixed with "is" such as isAutoCreateConfigTable). Usually I prefer:

#2547 report it
Arsеn at 2011/01/16 09:15am

default value for your table name

$configTableName = 'Config'

, change to

$configTableName = '{{Config}}'

it will be more flexible when using the prefix

#2263 report it
Trejder at 2010/12/03 09:46am
Code optimization?

I've also noticed that for optimization and performance reasons this part of code:

public function get($key)
        $db = $this->_getDb();
        $cache = $this->_getCache();
        if (null === $this->_config)
            $this->_getConfig($db, $cache);

Should be changed into:

public function get($key)
        if (null === $this->_config)
            $db = $this->_getDb();
            $cache = $this->_getCache();
            $this->_getConfig($db, $cache);

This way, calls to _getDb() and _getCache() will be executed only if $this->_config is NULL not (as in current code) with every call to get() function.

#2262 report it
Trejder at 2010/12/03 08:10am
Interesting coding approach! :)

This extension's author has some kind of interesting approach or coding technique! :) For example, look at this particular piece of code:

private function _getConfig($db, $cache)
    if (true === $this->autoCreateConfigTable)
    if (false === $cache || false === ($this->_config = $cache->get(self::CACHE_KEY)))
        $dbReader = $db->createCommand("SELECT * FROM '{$this->configTableName}`")->query();
        while (false !== ($row = $dbReader->read()))
            $this->_config[$row['key']] = $row['value'];
        if (false !== $cache)
            $cache->set(self::CACHE_KEY, $this->_config);

For the first time in my PHP coding history I see notation where TRUE or FALSE is on left side of equation mark! And this way all if are constructed in this code.

But this is only a coding technique, which anyone can use as he/she prefers! :)

#691 report it
Nique at 2010/03/17 02:56pm
Never mind

It works, my mistake.

#701 report it
Nique at 2010/03/16 10:39am
No auto create table

Hi Y!!,

Thank you for this extension. I have a problem with the setup. It doesn't auto create the table.

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: Other Open Source License
  • Developed by: Y!!
  • Category: Others
  • Votes: +15
  • Downloaded: 2,991 times
  • Created on: Jan 6, 2010
  • Last updated: Jan 8, 2010