Get last query run in Cakephp 2.x :
There is a situation when we need to check what was the last query which was executed in Cakephp. My purpose for writing this blog is to provide you the solution for this as I was also stuck here.
I wanted to get the last query CakePHP ran. I was not able to turn the debug mode on in core.php and was not able to run the code locally as well. So the best way which will get the last sql query is use the getDatasource
and getLog
method.
For Cake 2.x, you can test the following lines of code:
function getLastQuery() {
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
Thanks for reading the blog.
1 Comment(s)