简体   繁体   中英

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

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.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM