小编典典

cakephp在执行之前查看已编译的SQL查询

sql

我的查询在每次运行时都会收到超时错误。与连接的分页。
我想调试SQL,但是由于超时,所以看不到它。

在执行之前如何查看已编译的SQL查询?


一些蛋糕代码:

$this -> paginate = array(
        'limit' => '16',
        'joins' => array( array(
                'table' => 'products',
                'alias' => 'Product',
                'type' => 'LEFT',
                'conditions' => array('ProductModel.id = Product.product_model_id')
            )),
        'fields' => array(
            'COUNT(Product.product_model_id) as Counter',
            'ProductModel.name'
            ),
        'conditions' => array(
            'ProductModel.category_id' => $category_id,
        ),
        'group' => array('ProductModel.id')
    );

阅读 175

收藏
2021-03-10

共1个答案

小编典典

首先,将debug变量设置为2 in app/config/config.php

然后加:

<?php echo $this->element('sql_dump');?>

在布局的末尾。实际上,应该在默认的蛋糕布局中将其注释掉。

现在,您将能够看到所有转到数据库的SQL查询。

现在复制查询并在数据库上使用SQL
EXPLAIN
命令(MySQL的链接)来查看查询在DBMS中的作用。有关CakePHP调试的更多信息,请参见此处

由于您的脚本甚至无法渲染,因此您可以尝试直接从数据源获取最新的日志,方法是:

function getLastQuery()
{
    $dbo = $this->getDatasource();
    $logs = $dbo->getLog();
    $lastLog = end($logs['log']);
    return $lastLog['query'];
}

这需要在模型中,因为getDatasource()功能是在模型中定义的。检查整个$logs变量,然后查看其中的内容。

2021-03-10