How to get the latest run request in CakePHP 3.2?

I want to get the last executed request in CakePHP 3.2, I used the following in CakePHP 2.x: -

function getLastQuery() { Configure::write('debug', 2); $dbo = $this->getDatasource(); $logs = $dbo->getLog(); $lastLog = end($logs['log']); $latQuery = $lastLog['query']; echo "<pre>"; print_r($latQuery); } 

How can I do this in CakePHP 3.x?

+6
source share
2 answers

In plain English: all you have to do is change config / app.php

Find the Datasources configuration and set 'log' => true

 'Datasources' => [ 'default' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => 'localhost', ... 'log' => true, // Set this ] ] 

If your application is in debug mode, you will see an SQL query when an SQL error is displayed on your page. If you do not have debug mode, you can register SQL queries in a file by adding the following:

configurations /app.php

Find the Log configuration and add a new log type:

 'Log' => [ 'debug' => [ 'className' => 'Cake\Log\Engine\FileLog', 'path' => LOGS, 'file' => 'debug', 'levels' => ['notice', 'info', 'debug'], 'url' => env('LOG_DEBUG_URL', null), ], 'error' => [ 'className' => 'Cake\Log\Engine\FileLog', 'path' => LOGS, 'file' => 'error', 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], 'url' => env('LOG_ERROR_URL', null), ], // Add the following... 'queries' => [ 'className' => 'File', 'path' => LOGS, 'file' => 'queries.log', 'scopes' => ['queriesLog'] ] ], 

Your SQL queries will now be written to the log file, which you can find in /logs/queries.log

+9
source

Added database \ ConnectionManager :: get (). It replaces getDataSource ()

After Cookbook 3.0 you need to enable Query Logging and select a file or console.

 use Cake\Log\Log; // Console logging Log::config('queries', [ 'className' => 'Console', 'stream' => 'php://stderr', 'scopes' => ['queriesLog'] ]); // File logging Log::config('queries', [ 'className' => 'File', 'path' => LOGS, 'file' => 'queries.log', 'scopes' => ['queriesLog'] ]); 

Cookbook 3.0 - Query Logging

0
source

All Articles