Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter change database config at runtime

Tags:

codeigniter

Can I change the database config per method in a controller?

$db['default']['db_debug'] = TRUE;

The default is TRUE, while I need to make it false in a certain method to catch the error and do something else (for example show 404 page).

When I tried $this->config->load('database') it fails.

Another question :

Can I check an incorrect query and catch it to some variables rather than displaying it to users other than setting the db_debug config to FALSE?

like image 801
Henson Avatar asked Oct 19 '11 02:10

Henson


4 Answers

I checked the code of system/database/DB_Driver and found that:

$this->db->db_debug = FALSE;

will work in my controller to enable/disable the debug thing on the fly.

like image 148
Antonio Gallo Avatar answered Nov 19 '22 07:11

Antonio Gallo


Expanding on the answer by comenk, you can extend the database class and implement various methods by which to achieve your goal.

First, you'll need to extend the core Loader class by creating a MY_Loader.php file

class MY_Loader extends CI_Loader
{
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Load the Standard and/or Extended Database function & Driver class
     *
     * @access  public
     * @return  string
     */
    function database( $params = '', $return = FALSE, $active_record = NULL )
    {
        $ci =& get_instance();

        if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($ci->db) AND is_object($ci->db))
        {
            return FALSE;
        }

        $my_db      = config_item('subclass_prefix').'DB';
        $my_db_file = APPPATH.'core/'.$my_db.EXT;

        if(file_exists($my_db_file))
        {
            require_once($my_db_file);
        }
        else
        {
            require_once(BASEPATH.'database/DB'.EXT);
        }

        // Load the DB class
        $db =& DB($params, $active_record);

        $my_driver      = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver';
        $my_driver_file = APPPATH.'core/'.$my_driver.EXT;

        if(file_exists($my_driver_file))
        {
            require_once($my_driver_file);
            $db = new $my_driver(get_object_vars($db));
        }

        if ($return === TRUE)
        {
            return $db;
        }

        // Initialize the db variable.  Needed to prevent
        // reference errors with some configurations
        $ci->db = '';
        $ci->db = $db;
    }
}

By implementing the above this will allow you to create a MY_DB_mysqli_driver.php whereby mysqli is replaced by whatever driver you're using in your CI database.php config.

At this point you'd add comenk's answer to MY_DB_mysqli_driver.php

function debug_on() {
     return $this->db_debug = TRUE;
}

function debug_off() {
     return $this->db_debug = FALSE;
}

function in_error() {
     return (bool) $this->_error_number();
}

Then in your model/controller,

$this->db->debug_off();

$this->db->query('SELECT * FROM `table`');

if( $this->db->in_error() ) {
    show_404();
}

$this->db->debug_on();
like image 30
Ben Swinburne Avatar answered Nov 19 '22 07:11

Ben Swinburne


you must add function on system/database/DB_driver.php

function debug_on()
{
     $this->db_debug = TRUE;
     return TRUE;
}

function debug_off()
{
     $this->db_debug = FALSE;
     return FALSE;
}

after that you can simply do this command to changes at run-time

$this->db->debug_off();
$this->db->reconnect();
like image 22
comenk Avatar answered Nov 19 '22 07:11

comenk


$this->db->db_debug = 0; // 0: off, 1: on That worx for me... You can look at the $GLOBALS variable to locate this generic setting.

like image 20
Christian Kanakis Avatar answered Nov 19 '22 06:11

Christian Kanakis