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.

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

[mysql]

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

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

Downloads

show all

Related Extensions