MySQL CakePHP - 获取上次运行的查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2521225/
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 - get last query run
提问by Matt
I want to get the last query CakePHP ran. I can't turn debug on in core.php and I can't run the code locally. I need a way to get the last sql query and log it to the error log without effecting the live site. This query is failing but is being run.
我想得到 CakePHP 运行的最后一个查询。我无法在 core.php 中打开调试,也无法在本地运行代码。我需要一种方法来获取最后一个 sql 查询并将其记录到错误日志中而不影响实时站点。此查询失败但正在运行。
something like this would be great:
这样的事情会很棒:
$this->log($this->ModelName->lastQuery);
Thanks in advance.
提前致谢。
回答by Matt
For Cake 2.0, the query log is protected so this will work
对于 Cake 2.0,查询日志受到保护,因此这将起作用
function getLastQuery() {
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
回答by Haktan Suren
Tested in CakePHP v2.3.2
在 CakePHP v2.3.2 中测试
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
回答by Daniel Wright
In CakePHP 1.x, the data you want is accessible in DataSource::_queriesLog
. Cake doesn't really provide a getter method for this member, but the underlying language being PHP, nothing stops you from doing the following:
在 CakePHP 1.x 中,您想要的数据可以在DataSource::_queriesLog
. Cake 并没有真正为这个成员提供 getter 方法,但是底层语言是 PHP,没有什么能阻止你做以下事情:
In app/app_model.php
:
在app/app_model.php
:
function getLastQuery()
{
$dbo = $this->getDatasource();
$logs = $dbo->_queriesLog;
return end($logs);
}
回答by blavla
You can use this inline.
您可以使用此内联。
$dbo = $this->Model->getDatasource();
// store old state
$oldStateFullDebug = $dbo->fullDebug;
// turn fullDebug on
$dbo->fullDebug = true;
// Your code here! eg.
$this->Model->find('all');
// write to logfile
// use print_r with second argument to return a dump of the array
Debugger::log(print_r($dbo->_queriesLog, true));
// restore fullDebug
$dbo->fullDebug = $oldStateFullDebug;
回答by alinn
This is a very late answer, i know, but for whoever needs this in the future, you can always restrict setting debug to your IP, For example:
这是一个很晚的答案,我知道,但是对于将来需要它的人,您始终可以将设置调试限制为您的 IP,例如:
Configure::write('debug', 0);
if($_SERVER["REMOTE_ADDR"] == '192.168.0.100'){
Configure::write('debug', 2); //Enables debugging only for your IP.
}
回答by Krunoslav Djakovic
Combination of Matt's and blavia's solution (works when debug is not 2):
Matt 和 blavia 的解决方案的组合(当调试不是 2 时有效):
$dbo = $this->Model->getDatasource();
$oldStateFullDebug = $dbo->fullDebug;
$dbo->fullDebug = true;
// find or whatever...
$this->Model->find("all");
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
CakeLog::write("DBLog", $lastLog['query']);
$dbo->fullDebug = $oldStateFullDebug;
回答by Quy Le
Simple you can use showLog() function
简单的你可以使用 showLog() 函数
var_dump($this->YourModel->getDataSource()->showLog());
回答by David Yell
Having a quick skim of the book, cakephp api getLogyou could turn on logTransaction
. Although having not used it, I'm not sure how it will perform.
快速浏览一下这本书,你可以打开cakephp api getLoglogTransaction
。虽然没有使用过它,但我不确定它的表现如何。
Otherwise you could experiment with FirePHPand here is the a guide for it,
You might try DebugKit, although off the top of my head I think you do still need debug 2 to get it to work.
您可以尝试DebugKit,尽管我认为您仍然需要调试 2 才能使其正常工作。
Hopefully something might give you a lead. :)
希望有什么可以给你一个领先。:)
回答by ajeet
You can use this:
你可以使用这个:
$log = $this->Model->getDataSource()->getLog(false, false);
pr($log);die;
回答by krishna ragav
There are two methods to view the query in CakePHP.
在 CakePHP 中有两种查看查询的方法。
Both methods you have to add the below one line in app/Config/core.php
这两种方法都必须在 app/Config/core.php 中添加以下一行
Configure::write('debug', 2);
First methods :
第一种方法:
debug($this->getLastQuery());
where you want to get the query add above line and call this function getLastQuery()
on same controller using below code
您希望查询添加到上面的行并getLastQuery()
使用下面的代码在同一控制器上调用此函数
public function getLastQuery() {
$dbo = $this->TModel->getDatasource(); //Here TModel is a model.what table you want to print
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
second method :
第二种方法:
add the below line in the any Elements files.
在任何元素文件中添加以下行。
<?php echo $this->element('sql_dump'); ?>