cakephp在执行前看到编译的SQL Query [英] cakephp see the compiled SQL Query before execution
问题描述
我的查询在每次运行时都会得到超时错误。它的连接分页。
我想调试SQL,但是由于我得到一个超时,我看不到它。
My query gets the timeout error on each run. Its a pagination with joins.
I want to debug the SQL, but since I get a timeout, I can't see it.
可以在执行前看到编译的SQL查询吗?
How can I see the compiled SQL Query before execution?
一些蛋糕代码:
$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')
);
推荐答案
首先,设置在
变量为2。 app / config / config.php
中调试
First off, set the debug
variable to 2 in app/config/config.php
.
然后添加:
<?php echo $this->element('sql_dump');?>
在布局的最后。这实际上应该在您的默认蛋糕布局中注释掉。
at the end of your layout. This should actually be commented out in your default cake layout.
现在,您将可以看到数据库中的所有SQL查询。
现在复制查询并使用 SQL EXPLAIN 命令(链接用于MySQL),以查看该查询在 DBMS 中执行的操作。有关CakePHP调试的更多信息,请查看此处。
Now copy the query and use the SQL EXPLAIN command (link is for MySQL) over the database to see what the query does in the DBMS. For more on CakePHP debugging check here.
由于您的脚本甚至不能渲染,您可以尝试从数据源直接获取最新的日志:
Since your script doesn't even render you can try to get the latest log directly from the datasource with:
function getLastQuery()
{
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
这需要在一个模型中,因为 getDatasource ()
函数在模型中定义。
检查整个 $ logs
变量,看看有什么。
This needs to be in a model since the getDatasource()
function is defined in a model.
Inspect the whole $logs
variable and see what's in there.
这篇关于cakephp在执行前看到编译的SQL Query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!