Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last run query in CakePHP 3.2?

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?

like image 683
sradha Avatar asked Feb 09 '16 11:02

sradha


People also ask

How to view the query in CakePHP?

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 where you want to get the query add above line and call this function getLastQuery () on same controller using below code

How to call and run stored procedure using CakePHP method?

We need to call and run stored procedure using CakePHP method. Run raw query to select all rows from a table where name column rows contains a substring like jay value. Run raw query to select all rows from a table where id column value will be either any of these 3, 5, 8, 9 or name column value contains a substring esh as last characters.

What is the use of count () method in CakePHP?

One common use case for this is providing a cached value or an estimate of the total rows, or to alter the query to remove expensive unneeded parts such as left joins. This becomes particularly handy when using the CakePHP built-in pagination system which calls the count () method:

How to use join functions in CakePHP ORM query?

use CakePHP ORM query functions like contain () and matching () use join functions like innerJoin (), leftJoin (), and rightJoin () You should use contain () when you want to load the primary model, and its associated data.


1 Answers

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

like image 136
BadHorsie Avatar answered Oct 06 '22 02:10

BadHorsie