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

My Codeigniter system uses several databases. I don’t need every database on every page, so I load every connection into the right model, and then load the required models into 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 databases in my models:

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

I loaded all my databases into MY_Controller () (extension of the main controller). When I load them there, the profiler works fine. When I load them into my models, it does not display database queries.

Database queries are compiled in the method _compile_queries()internally system/libraries/Profiler.php. When my databases are loaded into the model, their objects are not loaded into the CI object. The code below is the first few lines of the method _compile_queries()and where it happens.

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 a CI profiler to display my queries when loading my databases into my models? Or, more specifically, how do I get database objects to load into the main CI object when the databases are loaded in the model.

+5
source share
1 answer

I would get an instance of the CI object and save your database there so that it 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;  
     }
}
+8
source

All Articles