CakePHP - get last query run

前端 未结 11 533
萌比男神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:50

    There are two methods to view the query in CakePHP.

    Both methods you have to add the below one line in app/Config/core.php

    Configure::write('debug', 2); 
    

    First methods :

    debug($this->getLastQuery()); 
    

    where you want to get the query add above line and call this function getLastQuery() on same controller using below code

    public function getLastQuery() {
        $dbo = $this->TModel->getDatasource();  //Here TModel is a model.what table you want to print 
        $logs = $dbo->getLog();
        $lastLog = end($logs['log']);
        return $lastLog['query'];
    }
    

    second method :

    add the below line in the any Elements files.

    element('sql_dump'); ?>
    

提交回复
热议问题