获取CodeIgniter Active Record的当前SQL代码

Who*_*yIn 34 php activerecord codeigniter

例如,我想转换这个;

$this->db->get('table');
Run Code Online (Sandbox Code Playgroud)

对此;

'SELECT * FROM table'
Run Code Online (Sandbox Code Playgroud)

这有什么功能吗?我搜索了CI的用户指南,但没有找到任何解决方案.

小智 60

你也可以使用$this->db->get_compiled_select().get_compiled_select()和之间的区别last_query()是,get_compiled_select()即使您不对数据库运行查询,也会生成查询字符串.

  • 我刚刚意识到如果你使用`_compile_select()`,你必须在执行查询后使用`$ this-> db - > _ reset_select();`.如果不这样做,CodeIgniter将不会清除查询,并且所有查询都将无效. (6认同)
  • `_compile_select()`不再起作用,因为它们在最新的CI2 Github仓库中保护了该功能.我多次使用这个功能,现在我被卡住了.也许有一个解决方案? (6认同)
  • 搜索CI提交历史记录后,我发现有一个新的公共方法; `get_compiled_select()`.http://codeigniter.com/nightly_user_guide/database/active_record.html#this-db-get-compiled-select (5认同)

sit*_*joe 32

尝试

echo $this->db->last_query();
Run Code Online (Sandbox Code Playgroud)

在您运行Active Record Query之后,它会吐出它为您运行的原始SQL.我经常使用它.