Is there a way that one can cause CakePHP to dump its SQL log on demand? I'd like to execute code up until a point in my controller and see what SQL has been run.
9 Answers
Try this:
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
http://api.cakephp.org/2.3/class-Model.html#_getDataSource
You will have to do this for each datasource if you have more than one though.

- 18,365
- 21
- 80
- 122

- 510,633
- 85
- 743
- 889
-
7I recently had to do this for a legacy 1.2 code base and the method is similar, but not the same: `$log = $this->Model->getDataSource()->showLog( false ); debug( $log )`. Just in case anyone else runs across this answer via a search. – Rob Wilkerson Sep 26 '11 at 12:26
-
9And you will need to set `Configure::write('debug', 2);` to get MySQL logging enabled. – Wirone Sep 26 '12 at 08:25
-
In case anyone else needs a quick ref for what the getLog() args do: https://api.cakephp.org/2.3/class-DboSource.html#_getLog – Ben Wilson Sep 20 '17 at 18:18
There are four ways to show queries:
This will show the last query executed of user model:
debug($this->User->lastQuery());
This will show all executed query of user model:
$log = $this->User->getDataSource()->getLog(false, false); debug($log);
This will show a log of all queries:
$db =& ConnectionManager::getDataSource('default'); $db->showLog();
If you want to show all queries log all over the application you can use in view/element/filename.ctp.
<?php echo $this->element('sql_dump'); ?>

- 1,120
- 1
- 17
- 23
If you're using CakePHP 1.3, you can put this in your views to output the SQL:
<?php echo $this->element('sql_dump'); ?>
So you could create a view called 'sql', containing only the line above, and then call this in your controller whenever you want to see it:
$this->render('sql');
(Also remember to set your debug level to at least 2 in app/config/core.php
)

- 3,964
- 5
- 30
- 45

- 4,073
- 3
- 29
- 46
for cakephp 2.0 Write this function in AppModel.php
function getLastQuery()
{
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
To use this in Controller Write : echo $this->YourModelName->getLastQuery();

- 1,128
- 13
- 16
In CakePHP 1.2 ..
$db =& ConnectionManager::getDataSource('default');
$db->showLog();

- 1,221
- 12
- 19
It is greatly frustrating that CakePHP does not have a $this->Model->lastQuery();. Here are two solutions including a modified version of Handsofaten's:
1. Create a Last Query Function
To print the last query run, in your /app_model.php file add:
function lastQuery(){
$dbo = $this->getDatasource();
$logs = $dbo->_queriesLog;
// return the first element of the last array (i.e. the last query)
return current(end($logs));
}
Then to print output you can run:
debug($this->lastQuery()); // in model
OR
debug($this->Model->lastQuery()); // in controller
2. Render the SQL View (Not avail within model)
To print out all queries run in a given page request, in your controller (or component, etc) run:
$this->render('sql');
It will likely throw a missing view error, but this is better than no access to recent queries!
(As Handsofaten said, there is the /elements/sql_dump.ctp in cake/libs/view/elements/, but I was able to do the above without creating the sql.ctp view. Can anyone explain that?)

- 126
- 2
- 3
What worked finally for me and also compatible with 2.0 is to add in my layout (or in model)
<?php echo $this->element('sql_dump');?>
It is also depending on debug variable setted into Config/core.php

- 4,653
- 38
- 47
Plugin DebugKit for cake will do the job as well. https://github.com/cakephp/debug_kit

- 5,117
- 3
- 40
- 37
If you are interested in some specific part of code, you can clear first the log, and then display only queries that happen after that point.
Also note that 'Model' below, is the actual class name, like User, Page etc.
//clear log (boolean $clear = true)
$this->Model->getDataSource()->getLog(false, true);
...
...
...
...
//Show log so far
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
exit;

- 4,643
- 1
- 41
- 38