Welcome Guest, Not a member yet? Register   Sign In
Database based Config library
#1

[eluser]steelaz[/eluser]
A while ago codearachnid posted his extension to CI base Config library to add config items from database table - wiki. I made some modification and been using it for most of my projects.

Since CI Config library is loaded automagically, there's no need to load it explicitly. To start using it, add this line in your controller (constructor is a good place):

Code:
$this->config->load_db_items();

load_db_items() method will check if "settings" table exists and create it if necessary. Then it will read all key => value pairs in the table and add them to Config object. After load_db_items() was called, you can access you database values just like regular items, i.e.:

Code:
$website_name = $this->config->item('website_name');

To add or update database item use save_db_item() method, i.e.:

Code:
$this->config->save_db_item('website_name', 'My Website');

To remove database item use remove_db_item() method, i.e.:

Code:
$this->config->remove_db_item('website_name');


To install, create /application/libraries/MY_Config.php and copy/paste the code below. Change table name if you want to.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* CodeIgniter Config Extended Library
*
* This class extends the config to a database. Based on class written by Tim Wood (aka codearachnid).
*
* @package       CodeIgniter
* @subpackage    Extended Libraries
* @author        Arnas Lukosevicius (aka steelaz)
* @link          http://www.arnas.net/blog/
*
*/

class MY_Config extends CI_Config
{
    /**
     * CodeIgniter instance
     *
     * @var object
     */
    private $CI = NULL;

    /**
     * Database table name
     *
     * @var string
     */
    private $table = 'settings';


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Load config items from database
     *
     * @return void
     */
    public function load_db_items()
    {
        if (is_null($this->CI)) $this->CI = get_instance();

        if (!$this->CI->db->table_exists($this->table))
        {
           $this->create_table();
        }

        $query = $this->CI->db->get($this->table);

        foreach ($query->result() as $row)
        {
            $this->set_item($row->key, $row->value);
        }

    }

    /**
     * Save config item to database
     *
     * @return bool
     * @param string $key
     * @param string $value
     */
    public function save_db_item($key, $value)
    {
        if (is_null($this->CI)) $this->CI = get_instance();

        $where = array('key' => $key);
        $found = $this->CI->db->get_where($this->table, $where, 1);

        if ($found->num_rows > 0)
        {
            return $this->CI->db->update($this->table, array('value' => $value), $where);
        }
        else
        {
            return $this->CI->db->insert($this->table, array('key' => $key, 'value' => $value));
        }
    }

    /**
     * Remove config item from database
     *
     * @return bool
     * @param string $key
     */
    public function remove_db_item($key)
    {
        if (is_null($this->CI)) $this->CI = get_instance();

        return $this->CI->db->delete($this->table, array('key' => $key));
    }

    /**
     * Create database table (using "IF NOT EXISTS")
     *
     * @return void
     */
    public function create_table()
    {
        if (is_null($this->CI)) $this->CI = get_instance();

        $this->CI->load->dbforge();

        $this->CI->dbforge->add_field("`id` int(11) NOT NULL auto_increment");
        $this->CI->dbforge->add_field("`updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP");
        $this->CI->dbforge->add_field("`key` varchar(255) NOT NULL");
        $this->CI->dbforge->add_field("`value` text NOT NULL");

        $this->CI->dbforge->add_key('id', TRUE);

        $this->CI->dbforge->create_table($this->table, TRUE);
    }
}

/* End of file MY_Config.php */
/* Location: ./application/libraries/MY_Config.php */


Messages In This Thread
Database based Config library - by El Forum - 10-11-2009, 12:53 PM
Database based Config library - by El Forum - 11-18-2009, 01:58 AM
Database based Config library - by El Forum - 11-18-2009, 02:19 AM
Database based Config library - by El Forum - 11-18-2009, 02:22 AM
Database based Config library - by El Forum - 11-18-2009, 02:39 AM
Database based Config library - by El Forum - 11-18-2009, 02:45 AM
Database based Config library - by El Forum - 11-18-2009, 02:52 AM
Database based Config library - by El Forum - 11-18-2009, 03:28 AM
Database based Config library - by El Forum - 11-18-2009, 03:31 AM
Database based Config library - by El Forum - 11-18-2009, 03:52 AM
Database based Config library - by El Forum - 11-18-2009, 03:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB