Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display my database queries in the Codeigniter Profiler when I load my databases in my models?

My Codeigniter system uses multiple databases. I don't need every database on every page, so I load each connection in the model where it's needed, then load the required models within each controller. The profiler does not display any of the queries from these databases when I load things this way.

This is how I load the databases in my models:

$this->Companies_db = $this->load->database( 'companies', TRUE, TRUE );

I used to load all my databases in MY_Controller() ( an extension of the core controller ). When I load them there, the profiler works fine. When I load them in my models, it displays no database queries.

The database queries are being compiled in the method _compile_queries() within system/libraries/Profiler.php. When my databases are loaded in the model, their objects are not loading into the CI object. The code below is the first few lines from the _compile_queries() method and is where this is happening.

foreach (get_object_vars($this->CI) as $CI_object)
    {
        if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
        {
            $dbs[] = $CI_object;
        }
    }

How do I get the CI profiler to display my queries when I load my databases in my models? Or, more specifically, how do I get my database objects to load into the main CI object when the databases are loaded in the models.

like image 889
T. Brian Jones Avatar asked Jul 10 '12 22:07

T. Brian Jones


1 Answers

I would get an instance of the CI-object and store your database there, so that is also available to the profiler.

class My_Model {
     private $Companies_db;

     public function __construct(){
           $this->Companies_db = $this->load->database( 'companies', TRUE, TRUE );

           // Pass reference of database to the CI-instance
           $CI =& get_instance();
           $CI->Companies_db =& $this->Companies_db;  
     }
}
like image 145
Zombaya Avatar answered Sep 18 '22 23:09

Zombaya