How to get the last query executed by CakePHP?

4

I want to get the last query executed by CakePHP

Example

$data  =  $this->Ticket->find('all',  array('conditions'  =>  $conditions,  'order'  =>  array('Ticket.id'  =>  'DESC')));

$this->query = $this->Ticket->getLastQuery();

public function getLastQuery($fromWhereClause = false){
        $dbo = $this->getDatasource();
        $logs = $dbo->getLog();
        $lastLog = end($logs['log']);
        if ($fromWhereClause) {
            return strstr($lastLog['query'], 'WHERE');
        }
        return $lastLog['query'];
}

but the getLastQuery is only executed when the debug is set to the value 2 I would like to get it set to 0

    
asked by anonymous 11.07.2014 / 23:41

1 answer

1

Try this:

$data  =  $this->Ticket->find('all',  array('conditions'  =>  $conditions,  'order'  =>  array('Ticket.id'  =>  'DESC')));

$this->query = $this->Ticket->getLastQuery();

public function getLastQuery($fromWhereClause = false){
    $dbo = $this->getDatasource();
    $dbo->fullDebug = true;
    $logs = $dbo->getLog();
    $lastLog = end($logs['log']);
    if ($fromWhereClause) {
        return strstr($lastLog['query'], 'WHERE');
    }
    $dbo->fullDebug = false;
    return $lastLog['query'];
}

Based on this SOen response: link

    
29.07.2014 / 18:01