9

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

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 it in CakePHP 3.x?

sradha
  • 2,216
  • 1
  • 28
  • 48
  • 2
    It's probably a little more work in CakePHP 3.x, you may need to use a custom query logger class. May I ask about your actual use case, ie why exactly do you need to have access to the last query? – ndm Feb 09 '16 at 11:48
  • 1
    I am facing some problem while saving data into db,so i need it. – sradha Feb 09 '16 at 12:22

3 Answers3

14

In plain English: All you need to do is modify 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 app is in debug mode, you will now see the SQL query when your page displays a SQL error. If you do not have debug mode on, you can log the SQL queries to a file by also adding the following:

config/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 a log file which you can find in /logs/queries.log

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
0

Database\ConnectionManager::get() has been added. It replaces getDataSource()

Following the Cookbook 3.0 you need to enable Query Logging and either choose file or console logging.

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

Curtis Gibby
  • 884
  • 1
  • 12
  • 25
om1
  • 166
  • 11
  • I am unable to use it. Please tell where will i keep this codes? – sradha Feb 16 '16 at 06:57
  • @sradha, *et al.*: [Configuring `Log` should be done during your application’s bootstrap phase.](http://book.cakephp.org/3.0/en/core-libraries/logging.html) Note that queries are logged at the "debug" log level, which is set up by default in `app.default.php` to write to a file in the LOGS directory (at `'Log' => ['debug' => [...] ]`), so if you copied at least that portion to your own `app.php`, then all you should need to do is either set `log` to `true` in your database configuration, or call `logQueries` (e.g., `ConnectionManager::get('default')->logQueries(true)`) . – Synexis Oct 18 '16 at 23:25
0

In Controller, We need to write two lines as follows

echo "<pre>";
print_r(debug($queryResult));die; 

It will show all result along with sql query syntax. Here we are using debug for show result.