Mic*_*ert 5 php cakephp cakephp-3.0
如何在CakePHP 3中查看model-> save()上的SQL查询?有没有办法做到这一点?我想获得特定的SQL查询,例如当我保存新实体时.
我需要它,因为我想在某些情况下将其保存到日志文件中.
我的bootstrap.php日志配置:
Log::config('current', [
'className' => 'File',
'path' => LOGS.DS.date('Y-m').DS,
'scopes' => ['daily','queriesLog'],
'file' => date('Y-m-d'),
]);
Run Code Online (Sandbox Code Playgroud)
我想得到什么:
例如,当我保存实体时:
$this->Clients->save($client);
Run Code Online (Sandbox Code Playgroud)
我想记录一些东西,我想用这个日志保存INSERT SQL查询
Log::warning('test\n', ['scope' => ['daily']]);
Run Code Online (Sandbox Code Playgroud)
解决方案可能是使用查询日志记录
http://book.cakephp.org/3.0/en/orm/database-basics.html#query-logging
您可以在保存模型时打开查询日志,然后将其关闭
例如我有一个评论模型
在我的 bootstrap.php 中我做了
Log::config('current',
[
'className' => 'File',
'path' => LOGS.date('Y-m').DS, // you don't need a DS between LOGS and date()
'scopes' => ['daily','queriesLog'],
'file' => date('Y-m-d'),
]);
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我做了
$conn = \Cake\Datasource\ConnectionManager::get('default');
$comment = $this->Comments->get(5620); // 5620 is the id of one comments of mine
$conn->logQueries(true);
$comment = $this->Comments->get(5619); // 5619 is onother id of one comments of mine
$comment->text = 'Test';
$this->Comments->save($comment);
$conn->logQueries(false);
Run Code Online (Sandbox Code Playgroud)
这样,在日志文件夹中创建了一个文件,该文件包含以下内容
2015-12-16 13:38:35 Debug: SELECT ... WHERE Comments.id = 5619 LIMIT 1
2015-12-16 13:38:35 Debug: BEGIN
2015-12-16 13:38:35 Debug: UPDATE comments SET text = 'Test' WHERE id = 5619
2015-12-16 13:38:35 Debug: COMMIT
Run Code Online (Sandbox Code Playgroud)
请注意,用于获取评论 #5620 的查询尚未记录
另请注意,如果您没有启用 debugkit,这会起作用