settings Another way to store configuration items to database

  1. Requirements
  2. Instalation
  3. Usage
  4. Notes & Changelog

This extension is an alternative to my "myconfig" extension from here: http://www.yiiframework.com/extension/myconfig/ but it uses only the database+caching.

As explained in this comment http://www.yiiframework.com/extension/myconfig/#c3727 using this extension has some advantages and some extra requirements.

Requirements

1) Yii 1.1.x
2) A Cache component activated (CFileCache will do it just fine)

Instalation

Add the component to the main.php config file:

[...]
'cache'=>array(
            'class'=>'system.caching.CFileCache',
        ),
'settings'=>array(
        'class'     	        => 'CmsSettings',
        'cacheComponentId'	=> 'cache',
        'cacheId'   		=> 'global_website_settings',
        'cacheTime' 		=> 84000,
        'tableName'		=> '{{settings}}',
        'dbComponentId'		=> 'db',
        'createTable'		=> true,
        'dbEngine'		=> 'InnoDB',
        ),
[...]

Usage

/*   
* Set a database item:  
* $itemName can be an associative array() in key=>value pairs  ($itemValue="" in this case) 
*/
Yii::app()->settings->set($categoryName, $itemName, $itemValue, $toDatabase=true);  

// Get a database item:  
Yii::app()->settings->get($categoryName, $itemName);  

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

// Delete a database item:  
Yii::app()->settings->delete($categoryName, $itemName);  

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

//Import items from a file:  
$items=include('path/to/file.php');//returns an array() in key=>value pairs  
Yii::app()->settings->set($categoryName, $items);  

The component uses something like "lazy loading" for loading items within a category, meaning that the items from a category will be loaded when you request them first time. Beside this, at the end of the request, the items from that requested category are written to cache, so next time when you request them, they will be served from cache.

Note, the component is smart enough to know itself when a new item/category has been added and refresh the cache accordingly so you don't have to keep track of the items you add to database.

Basically, in most of the cases, the database will be hit only once, then all items will be served from cache, which means you will get a nice way to manage the project configuration without performance penalties.

Notes & Changelog

Version 1.1.c
Various improvements

Version 1.1.d
-> Contains small performance improvements.
-> You can now use the get() method like

$retrieve_custom_settings=Yii::app()->settings->get('system',array('admin_email','contact_email','my_email'=>'some default value'));

In the above example, $retrieve_custom_settings becomes an array having the 'admin_email' and 'contact_email' keys. If these values are empty or they don't exists in database then they will be set to null otherwise you will retrieve their values. It is set this way so that you can safely use $retrieve_custom_settings['admin_email'] even if it doesn't exists.

Version 1.1.e
->Added setters/getters for all the public properties.
->The component supports the automatic creation of the database table (optionally, you can specify which storage engine to use in case you use MYSQL)
->Added the option to specify the name of the Cache/Database components via the $cacheComponentId and $dbComponentId properties.
->removed the __call() magic function.
->commented the class methods.

Special thanks goes to Gustavo who helped me allot with this version.

Upgrading to this version will not break the backward compatibility in any way(hope you didn't make use of the __call() method) and it is recommended to do so.
In case you use the autocreate database table option, don't forget to turn it off after the table has been created.

Version 1.2
-> added the deleteCache() public method to allow to delete cached categories
-> implemented a cache registry that will hold all the cached categories and it will be updated each time a new category is loaded/deleted Usage:
-> various changes that won't break BC.

Yii::app()->settings->deleteCache('categoryName'); //delete a single category  
Yii::app()->settings->deleteCache(array('c1', 'c2','c3')); // delete multiple categories  
Yii::app()->settings->deleteCache(); // delete all cached categories.

Please let me know in case you find any error.

38 0
63 followers
4 240 downloads
Yii Version: 1.1
License: (not set)
Category: Database
Developed by: twisted1919
Created on: May 23, 2011
Last updated: 6 years ago

Downloads

show all

Related Extensions