• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Config Class Extend to Database

#1
[eluser]Code Arachn!d[/eluser]
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.
Code:
<?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.
Code:
$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:
Code:
$config['dbt_site_config']        = 'site_config';

Seen in the wiki


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.