CakePHP - get last query run

前端 未结 11 539
萌比男神i
萌比男神i 2020-12-02 13:33

I want to get the last query CakePHP ran. I can\'t turn debug on in core.php and I can\'t run the code locally. I need a way to get the last sql query and log it to the erro

11条回答
  •  甜味超标
    2020-12-02 13:38

    Combination of Matt's and blavia's solution (works when debug is not 2):

    $dbo = $this->Model->getDatasource();
    $oldStateFullDebug = $dbo->fullDebug;
    $dbo->fullDebug = true;
    // find or whatever...
    $this->Model->find("all");
    $logs = $dbo->getLog();
    $lastLog = end($logs['log']);
    CakeLog::write("DBLog", $lastLog['query']);
    $dbo->fullDebug = $oldStateFullDebug;
    

提交回复
热议问题