在控制器cakephp 3中打印最后执行的查询

时间:2017-01-05 18:58:43

标签: cakephp-3.0

假设我在cakephp 3中有一个查询

$post = $this->Posts->get($id, [
        'contain' => ['Postmeta']
    ]);

我想像普通的mysql查询一样打印它,例如

SELECT * FROM posts....

任何人都可以解释一下我怎么能实现这个目标。请仅在cakephp 3环境中回答。是否可以将其作为普通的mysql查询在控制器中打印?请不要提及queries.log文件解决方案。因为是时候打开文件并在queries.log文件中的每个执行的查询之后看到查询,看起来是cakephp风格 感谢

4 个答案:

答案 0 :(得分:0)

在Controller中,我们需要在查询代码后写两行,如下所示

$post = $this->Posts->get($id, [
   'contain' => ['Postmeta']
]);
echo "<pre>";
print_r(debug($post));die; 

它将显示所有结果以及sql查询语法。

在这里,我们将debug用于显示结果以及sql查询。

答案 1 :(得分:-1)

出于调试原因:cakephp SQL Log。

cakephp SQL Log

答案 2 :(得分:-2)

<强>查询:

$post = $this->Posts->get($id, [
    'contain' => ['Postmeta']
]);

对于上述查询的打印/获取SQL语句,您可以在控制器中使用debug()函数:

$post = $this->Posts->get($id, [
    'contain' => ['Postmeta']
]);
debug($post);

答案 3 :(得分:-2)

只需写下:

die(print_r($post));