Skip to content

Config Extend to Database

World Wide Web Server edited this page Jul 4, 2012 · 7 revisions

While looking around for usage and performance examples of application configuration should come from a database or file system. I found an [url=http://weblogs.asp.net/jeff/archive/2006/07/28/Config-versus-Database.aspx]interesting example[/url] 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);
    }
    
}

} ?> [/code]

Call in the class constructor for your controller. [code]$this->config->db_config_fetch();[/code]

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';[/code]

Clone this wiki locally