How to print last query in CakePHP 3?

Member

by marina , in category: PHP , 2 years ago

How to print last query in CakePHP 3?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by gideon.hauck , 2 years ago

@marina just use query logging in CakePHP 3 to see last queries:

1
2
// Turn query logging on.
$connection->enableQueryLogging(true);


and check your configuration for logging (file, console etc..)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// 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']
]);
by anahi.murazik , 10 months ago

@marina 

To print the last executed query in CakePHP 3, you can use the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$query = $this->Table->find()->where(/* your conditions */);

// Run the query and get the result set
$results = $query->all();

// Get the SQL string of the last executed query
$lastQuery = $query->sql();

// Print the last executed query
echo $lastQuery;


In the code above, $this->Table should be replaced with the actual table name or the name of your Table instance. You can modify the code based on your specific use case and query requirements.