我想在Cake
PHP 3.2中获取最后执行的查询,我之前在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);
}
我怎么能在CakePHP 3.x中做到这一点?
用简单的英语:你需要做的就是修改config / app.PHP
找到数据源配置并设置’log’=>真正
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection','driver' => 'Cake\Database\Driver\MysqL','persistent' => false,'host' => 'localhost',...
'log' => true,// Set this
]
]
如果您的应用程序处于调试模式,您将在页面显示sql错误时看到SQL查询.如果未启用调试模式,则可以通过添加以下内容将SQL查询记录到文件中:
配置/ app.PHP
找到日志配置并添加新的日志类型:
'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','file' => 'error','levels' => ['warning','error','critical','alert','emergency'],'url' => env('LOG_ERROR_URL',// Add the following...
'queries' => [
'className' => 'File','file' => 'queries.log','scopes' => ['queriesLog']
]
],
您的SQL查询现在将写入日志文件,您可以在/logs/queries.log中找到该日志文件