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

僤鯓⒐⒋嵵緔 提交于 2020-01-01 11:36:09

问题


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.


回答1:


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;  
     }
}


来源:https://stackoverflow.com/questions/11423245/how-can-i-display-my-database-queries-in-the-codeigniter-profiler-when-i-load-my

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!