Take the 2-minute tour ×

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.

something like this would be great:


Thanks in advance.

share|improve this question
add comment

6 Answers

For Cake 2.0, the query log is protected so this will work

function getLastQuery() {
  $dbo = $this->getDatasource();
  $logs = $dbo->getLog();
  $lastLog = end($logs['log']);
  return $lastLog['query'];
share|improve this answer
add comment

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:

In app/app_model.php:

function getLastQuery()
    $dbo = $this->getDatasource();
    $logs = $dbo->_queriesLog;

    return end($logs);
share|improve this answer
When I try using this function I only get "2010-03-29 11:13:29 Error: " in the error log file. I used $this->log($this->Job->getLastQuery()); –  Phantz Mar 29 '10 at 0:36
$this->log(var_export($this->Job->getLastQuery(),true)); –  Phantz Mar 29 '10 at 0:54
Where are you calling $this->log()? Also, you might want to debug the output from Model::getLastQuery before throwing it in the log. It might be returning an array, causing an array-to-string conversion error. –  Daniel Wright Mar 29 '10 at 2:21
I had to do: $query = end($logs); return $query['query']; Either the format of the _queriesLog changed, or the above code returned an array of data about the query rather than simply the query itself. –  Brad Koch Aug 24 '11 at 19:06
add comment

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.

// 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;
share|improve this answer
add comment

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:

Configure::write('debug', 0);
if($_SERVER["REMOTE_ADDR"] == ''){ Configure::write('debug', 2);   
share|improve this answer
you can even make a debug method in one of your controllers that sets a session variable true/false and sets the debug config variable accordingly. –  alinn Dec 10 '10 at 8:00
Alinn, if access to that method is not secured properly it could lead to a security exploit where anyone could enable debug mode. Be careful. –  Brad Koch Jan 15 '13 at 16:38
add comment

Having a quick skim of the book, http://api.cakephp.org/2.3/class-DboSource.html#_getLog you could turn on 'logTransaction'. Although having not used it, I'm not sure how it will perform.

Otherwise you could experiment with FirePHP http://www.firephp.org/ and here is the a guide for it, http://bakery.cakephp.org/articles/view/debugging-with-firephp

You might try DebugKit, http://github.com/cakephp/debug_kit although off the top of my head I think you do still need debug 2 to get it to work.

Hopefully something might give you a lead. :)

share|improve this answer
add comment

Tested in CakePHP v2.3.2

share|improve this answer
add comment

Your Answer

