CakePHP 2.x Getting last Query

Getting last query on CakePHP when you need to exit in the middle of the action is pretty annoying. You would usually do a function where the dataSource would be instantiated and then you would access the queryLog.

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

return end($logs);
}

On CakePHP 2.x the _queriesLog is protected so you can’t access it directly. After a little search found out a great way to do it:

 $log = $this->Model->getDataSource()->getLog(false, false); debug($log);

This was explained in this post: http://blog.tersmitten.nl/archives/1172 and it does works wonders. All you need to do is to change the "Model" for your model name.

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: