capplicationsettings Read / write config settings from the DB like ActiveRecord

  1. About caching
  2. Requirements
  3. Usage
  4. Categories (Since version 1.1)
  5. More complex example
  6. Tip
  7. Resources

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.


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


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`)

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

      'applications' => array(

These are default values for Component or Application level:

    //'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:


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:

//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

      'cache' => false;
      'connection' => 'db';
      'settingsTable' => 'settings';
      'settingsIdColumn' => 'id';
      'settingsValueColumn' => 'value';


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.


4 0
Yii Version: Unknown
License: GPL-3.0
Category: Database
Developed by: Jorgee
Created on: Jul 30, 2013
Last updated: 7 years ago


show all

Related Extensions