cakephp 在执行前查看已编译的 SQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16780482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cakephp see the compiled SQL Query before execution
提问by yossi
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?
如何在执行前查看已编译的 SQL 查询?
Some cake code:
一些蛋糕代码:
$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')
);
回答by Borislav Sabev
First off, set the debug
variable to 2 in app/config/config.php
.
首先,将debug
变量设置为 2 in app/config/config.php
。
Then add:
然后加:
<?php echo $this->element('sql_dump');?>
at the end of your layout. This should actually be commented out in your default cake layout.
在您的布局结束时。这实际上应该在您的默认蛋糕布局中注释掉。
You will now be able see all SQL queries that go to the database.
您现在将能够看到进入数据库的所有 SQL 查询。
Now copy the query and use the SQL EXPLAINcommand (link is for MySQL) over the database to see what the query does in the DBMS. For more on CakePHP debugging check here.
现在复制查询并在数据库上使用SQL EXPLAIN命令(链接适用于 MySQL)以查看查询在DBMS 中的作用。有关 CakePHP 调试的更多信息,请查看此处。
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'];
}
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.
这需要在模型中,因为getDatasource()
函数是在模型中定义的。检查整个$logs
变量,看看里面有什么。
回答by Ninad Desai
One more thing you can do is ....
你还可以做的一件事是......
Go to Cake/Model/DataSource/DboSource.php and locate function execute() and print $sql variable. That should print the sql.
转到 Cake/Model/DataSource/DboSource.php 并找到函数 execute() 并打印 $sql 变量。那应该打印sql。
This certainly is not be the cleanest way (as you are changing Cake directory) .. but certainly would be quickest just to debug if something is not working with sql.
这当然不是最干净的方法(因为您正在更改 Cake 目录)..但是如果某些东西不能与 sql 一起使用,当然是最快的调试。
回答by Indrajeet Singh
Try...
function getLastQuery($model) {
$dbo = $model->getDatasource();
$logData = $dbo->getLog();
$getLog = end($logData['log']);
echo $getLog['query'];
}
回答by Faisal
Simple way to show all executed query of your given model:
显示给定模型的所有已执行查询的简单方法:
$sqllog = $this->ModelName->getDataSource()->getLog(false, false);
debug($sqllog);
回答by Sadee
class YourController extends AppController {
function testfunc(){
$this->Model->find('all', $options);
echo 'SQL: '.$this->getLastQuery();
}
function getLastQuery()
{
$dbo = ConnectionManager::getDataSource('default');
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
}
or you can get all the query by adding following line in to the function execute() in lib/Cake/Model/DataSource.php
或者您可以通过将以下行添加到 lib/Cake/Model/DataSource.php 中的函数 execute() 来获取所有查询
Debugger::dump($sql);
回答by palkesh
set the debug variable to 2 in app/config/config.php.
在 app/config/config.php 中将调试变量设置为 2。
echo $this->Payment->save();
Out put like =>SQL Query: INSERT INTO photoora_photoorange
.payments
VALUES (*******)
输出像 =>SQL Query: INSERT INTO photoora_photoorange
。payments
价值观 (*****)
[insert query][2]
[插入查询][2]