capplicationsettings

Read / write config settings from the DB like ActiveRecord
10 followers

This extension allows you to read / write parameters from the DB in an Active Record way, and allowed to declare different applications. That is, you can have forum, blogs, main, with different configurations for database, table, property name column, property value column, and cache option.

This is my first extension, so it is beta for the moment. It is functional with no caching for now, and I will be developing it actively the next days, so I hope you guys can test and give me feedback or help with code corrections.

About caching

Caching is in progress, so not available at the moment.

Requirements

No requirements. (Obviously a Database where you store your config settings).

Usage

First step for usage.

Create the table:

(Note that the column names and definitions are configurable later for the extension.)

CREATE TABLE `prefix_settings` (
  `id` varchar(50) NOT NULL DEFAULT '',
  `value` varchar(255) DEFAULT NULL,
  `category` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`,`category`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Then, in your config file (normally protected/config/main.php) add the following component:

'settings'=>array(
      'class'=>'CApplicationSettings',
      'applications' => array(
        'main'=>array(
        ),
      );
    ),

These are default values for Component or Application level:

'settings'=>array(
    //'cache' => false;
    //'connection' => 'db';
    //'settingsTable' => 'settings';
    //'settingsIdColumn' => 'id';
    //'settingsValueColumn' => 'value';
    //'applications' = array(),
    //'settingsCategoryColumn' = null,
    ),

Given that config, the component will be searching items from 'db' connection, 'settings' table, with a property ID (name) column called id, and a property value column called value.

Then, finally you can get the value:

Yii::app()->settings->main->property;

Or set a value:

Yii::app()->settings->main->property = 'value';

Categories (Since version 1.1)

Since version 1.1, there are "categories". What is it? Well, there was a need for having all settings in only one table. Suppose you have this schema:

Table modules_settings
name --- value --- module

Well, you can now declare that an application is a category of another.

For example:

'settings'=>array(
  'class'=>'CApplicationSettings',
  'settingsIdColumn'=>'name',
  'applications'=>array(
    'main'=>array(
      'settingsTable'=>'{{module_setting}}',
      'settingsCategoryColumn'=>'module',
    ),
    'forum'=>array(
      'isCategoryOf'=>'main'
    ),
    'blog'=>array(
      'isCategoryOf'=>'main'
    )
  ),
),
//This way you can access forum_home_url these ways:
//`module` comes from 'settingsCategoryColumn'=>'module',
 
Yii::app()->settings->forum->forum_home_url  // Condition will be 'name = `forum_home_url` AND module = `forum`'
 
Yii::app()->settings->main->forum_home_url  // Condition will be 'name = `forum_home_url`'
 
Yii::app()->settings->blog->forum_home_url  // Will throw Exception because doesn't exists in condition 'module = `blog`'

More complex example

'settings'=>array(
      'class'=>'CApplicationSettings',
      'cache' => false;
      'connection' => 'db';
      'settingsTable' => 'settings';
      'settingsIdColumn' => 'id';
      'settingsValueColumn' => 'value';
      'applications'=>array(
        'main'=>array(
        ),
        'forum'=>array(
          'connection'=>'otherDb',
          'settingsTable'=>'config',
          'settingsIdColumn'=>'properties',
          'settingValueColumn'=>'values',
        ),
        'blog'=>array(
          'settingsTable'=>'blog_settings',
        )
    ),

Tip

If you see an error message like: Column name must be string or array when setting a value, you must declare an index like Primary Key in your settings table.

Resources

Total 4 comments

#16264 report it
Jorgee at 2014/02/04 08:22am
Created GitHub

I've created a GitHub project if anyone wants to contribute. Note that I don't actively develop the extension, so contribs will be appreciated. https://github.com/FusionDev-OpenSource/yii-capplicationsettings

#15436 report it
selorm at 2013/11/09 12:47pm
works thanks

Nice Extension...Very useful. Hope for more features

#14669 report it
Jorgee at 2013/09/02 11:53am
Updated to 1.1

Updated to version 1.1 Please report any bugs at http://tracker.fusiondev.com.ar

#14332 report it
zainiafzan at 2013/08/05 03:55am
Looks Great..

Not try it yet...but its look awesome.. Thanks for sharing..

Leave a comment

Please to leave your comment.

Create extension