rizidoro
rizidoro

Reputation: 13428

retrieve sql query from cakephp finder method

I'm creating a behavior that log to a table the sql query executed by a particular Model in a controller. Looking for a method to return me the sql query executed for a particular finder method (like $this->MyModel->find('all') ) I found on the bakery that I can use $this->MyModel->find('sql'), but doesn't work for me. Someone knows how can I achieve this?

Thanks in advance

Upvotes: 1

Views: 1934

Answers (2)

elboletaire
elboletaire

Reputation: 5367

You can put this function in your app_model.php:

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

    return $logs;
}

And call it from any model ($this->getLastQueries()) or controller ($this->Model->getLastQueries()) to get them.

Upvotes: 1

kaptron
kaptron

Reputation: 477

$this->Model->find('sql') is not supported natively by Cake. You have to follow the rest of the instructions in the Bakery article for installing a new DBO driver, and adding support for the find('sql') method in your AppModel. Once you do this, it should be able to get you what you're looking for.

http://bakery.cakephp.org/articles/grant_cox/2008/06/23/get-the-find-query-sql-rather-than-query-result

Upvotes: 0

Related Questions