当我在模型中加载数据库时,如何在Codeigniter Profiler中显示我的数据库查询? [英] How can I display my database queries in the Codeigniter Profiler when I load my databases in my models?
问题描述
我的Codeigniter系统使用多个数据库。我不需要每个页面上的每个数据库,所以我加载每个连接在模型中需要它,然后在每个控制器中加载所需的模型。
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 );
我用来加载MY_Controller()(核心控制器的扩展)中的所有数据库。当我加载它们,分析器工作正常。当我在我的模型中加载它们时,它不显示数据库查询。
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.
数据库查询正在 _compile_queries c $ c>在
system / libraries / Profiler.php
中。当我的数据库在模型中加载时,它们的对象不会加载到CI对象中。下面的代码是 _compile_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;
}
}
如何获取CI分析器在我的模型中加载数据库时显示我的查询或者,更具体地说,当数据库加载到模型中时,如何将我的数据库对象加载到主CI对象中。
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对象的实例,并将您的数据库存储在那里,这样也可以用于分析器。
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;
}
}
这篇关于当我在模型中加载数据库时,如何在Codeigniter Profiler中显示我的数据库查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!