phpdatabasecodeignitermodelclass-extensions

Extending CI_Model for use with report database


I am trying to add a separate report database to my application by extending CI_Model and setting up the correct database to use based on if the database is defined. The report database will be a replicated version of the regular database for performance reasons.

Is this the proper way?

In application/core/MY_Model:

<?php
class MY_Model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        include APPPATH.'config/database.php';

        //If we have a reporting database load it and use it for all reporting functions
        if (isset($db['reports']))
        {
            $this->report_db = $this->load->database('reports', TRUE);  
        }
        else
        {
            $this->report_db = $this->load->database('default', TRUE);  
        }
    }

}

?>

Solution

  • You can use both of your databases within your code. This you have to define in config/database.php

    database.php

    $active_group = 'default';
    $active_record = TRUE;
    
    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'username';
    $db['default']['password'] = 'password';
    $db['default']['database'] = 'database1';
    $db['default']['dbdriver'] = 'mysqli';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = TRUE;
    $db['default']['cache_on'] = FALSE;
    $db['default']['cachedir'] = '';
    $db['default']['char_set'] = 'utf8';
    $db['default']['dbcollat'] = 'utf8_general_ci';
    $db['default']['swap_pre'] = '';
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE;
    
    $db['reports']['hostname'] = 'localhost';
    $db['reports']['username'] = 'username';
    $db['reports']['password'] = 'password';
    $db['reports']['database'] = 'database2';
    $db['reports']['dbdriver'] = 'mysqli';
    $db['reports']['dbprefix'] = '';
    $db['reports']['pconnect'] = TRUE;
    $db['reports']['db_debug'] = TRUE;
    $db['reports']['cache_on'] = FALSE;
    $db['reports']['cachedir'] = '';
    $db['reports']['char_set'] = 'utf8';
    $db['reports']['dbcollat'] = 'utf8_general_ci';
    $db['reports']['swap_pre'] = '';
    $db['reports']['autoinit'] = TRUE;
    $db['reports']['stricton'] = FALSE;
    

    reports_model.php

    <?php
    class Reports_model extends CI_Model
    {
        public function __construct()
        {
           parent::__construct();
           $this->db_reports = $this->load->database('reports', TRUE);
        }
    
        public function reports_list()
        {
            $this->db_reports->get('some_table');
        }
    }
    

    other_model.php

    <?php
    class Other_model extends CI_Model
    {
        public function __construct()
        {
           parent::__construct();
        }
    
        public function xyz()
        {
            $this->db->get('some_table');
        }
    }
    

    Explanation:

    Since you have defined default as your active group, so whatever database you have defined in your default group, you can use as following in your model -

    $this->db->get('some_table');   // as we defined normally
    

    And sine you have other databases also you can have another group of them. We have defined them in reports group. So we have add an extra _reports after db like this -

    $this->db_reports->get('some_table');