当我在模型中加载数据库时,如何在Codeigniter Profiler中显示数据库查询?

时间:2022-03-24 10:35:18

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.

我的Codeigniter系统使用多个数据库。我不需要每个页面上的每个数据库,因此我在模型中加载每个连接,然后在每个控制器中加载所需的模型。当我以这种方式加载时,探查器不会显示来自这些数据库的任何查询。

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.

我曾经在MY_Controller()(核心控制器的扩展)中加载我的所有数据库。当我在那里加载它们时,分析器工作正常。当我在模型中加载它们时,它不显示任何数据库查询。

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.

数据库查询正在system / libraries / Profiler.php中的_compile_queries()方法中编译。当我的数据库加载到模型中时,它们的对象不会加载到CI对象中。下面的代码是_compile_queries()方法的前几行,也就是发生这种情况的地方。

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.

当我在模型中加载数据库时,如何让CI探查器显示我的查询?或者,更具体地说,当数据库加载到模型中时,如何将数据库对象加载到主CI对象中。

1 个解决方案

#1


8  

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

我会得到一个CI对象的实例并将数据库存储在那里,因此探测器也可以使用它。

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

#1


8  

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

我会得到一个CI对象的实例并将数据库存储在那里,因此探测器也可以使用它。

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