Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by Code Arachn!d )

Config Extend to Database

While looking around for usage and performance examples of application configuration should come from a database or file system. I found an interesting example between the pros and cons of each. So whipped out the trusty IDE (PHP Eclipse) and whipped up this simple addition to the core of CI.

<?php
class MY_Config extends CI_Config {

/**
* CodeIgniter Config Extended Library
*
* This class extends the config to a database
*
* @package        CodeIgniter
* @subpackage    Extended Libraries
* @category    Extended Libraries
* @author        Tim Wood (aka codearachnid)
* @link        http://www.codearachnid.com/ci/db_fetch
*
* you must have the following structure setup in order to use this class
*
    CREATE TABLE IF NOT EXISTS `site_config` (
      `config_id` int(11) NOT NULL auto_increment,
      `last_updated` timestamp NOT NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
      `config_key` varchar(60) NOT NULL,
      `config_value` longtext NOT NULL,
      PRIMARY KEY  (`config_id`)
    ) ENGINE=MyISAM;
*
*
*/

    
function MY_Config()
    
{
        parent
::CI_Config();
    
}
    
    
function db_config_fetch()
    
{
        $CI
=& get_instance();
        
$query = $CI->db->get($this->item('dbt_site_config'));
        
        foreach (
$query->result() as $row)
        
{
            $this
->set_item($row->config_key, $row->config_value);
        
}
        
    }
    
}
?>


Call in the class constructor for your controller.

$this->config->db_config_fetch();

One last thing - either update where the database name is called from the static var file in the class or just add this to your config file:

$config['dbt_site_config']        = 'site_config';