This forum topic is related to http://www.yiiframework.com/extension/settings/
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.
A Cache component activated (CFileCache will do it just fine)
Your DB component should have the $tablePrefix property defined, even if it is empty:
'db'=>array( [...] 'tablePrefix' => '',//or fill it with your own prefix. [...] ),
This extension is designed for performance mostly, so things like automatically create the database table, or having the table name dynamically set in the extension configuration are out of discussion.
- Create a database table, named settings:
CREATE TABLE IF NOT EXISTS `settings` ( `id` int(11) NOT NULL auto_increment, `category` varchar(64) NOT NULL default 'system', `key` varchar(255) NOT NULL, `value` text NOT NULL, PRIMARY KEY (`id`), KEY `category_key` (`category`,`key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
- Add the component to the main.php config file:
[...] 'cache'=>array( 'class'=>'system.caching.CFileCache', ), 'settings'=>array( 'class' => 'CmsSettings', 'cacheId' => 'global_website_settings', 'cacheTime' => 84000, ), [...]
/* * 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
This version has been improved and it is recommended to update your existing component with this one. The code is fully compatible, so all you have to do is to replace the old component with this new one and everything will work just fine.
-> 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.
-> Now, this is more like a feature that is disabled by default.
Because some of you don’t preffer
Yii::app()->settings->set('catName',$array_of_items); //or Yii::app()->settings->get('catName',$array_of_items);
i am experimenting a new way of setting/getting items into categories.
Let’s assume we need a category named “movies”, with the new feature, we can set/get items like:
Yii::app()->settings->setMovies($array_of_items); //or Yii::app()->settings->getMovies($array_of_items); Yii::app()->settings->getMovies('some_key','some_default_value');
In case you need this feature enabled, uncomment the __call() method on line 165 and please provide feedback on using the extension this way.