我正在尝试在我的第一个严肃的 CodeIgniter 应用程序中调试一些代码,但我似乎找不到在哪里可以简单地获取我的 ActiveRecord 代码刚刚生成的原始 SQL。
$where = 'DAY(`datetime_start`) = '. date('d',$day) .' AND ';
$where .= 'MONTH(`datetime_start`) = '. date('m',$day) .'';
$this->db->from('events')->where($where);
$result = $this->db->get();
在查询运行之前:
$this->db->_compile_select();
运行后:
$this->db->last_query();
当然,我在发布后 2 分钟就找到了它,感谢 Phil Sturgeon。
echo $this->db->last_query();
此外,您可以将以下内容放入控制器中:
$this->output->enable_profiler(TRUE);
您会收到询问以及更多信息。
对于任何发现这篇旧文章并想知道最新 v3 中这是什么的人,函数是 $this->db->get_compiled_select()。
在 CI 的下一个版本发布之前你可以做这样的事情
private function get_query_string()
{
return 'SELECT ' .
implode( ' , ' , $this->db->ar_select ) .
' FROM ' .
implode( ' , ' , $this->db->ar_from ) .
( count( $this->db->ar_where ) ? ' WHERE ' : '' ) .
implode( ' ' , $this->db->ar_where );
}