如何在CakePHP 3中的model-> save()上获取SQL查询? [英] How to get SQL Query on model->save() in CakePHP 3?

查看:326
本文介绍了如何在CakePHP 3中的model-> save()上获取SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在CakePHP 3中查看model - > save()上的SQL查询?有什么办法吗?我想要获取特定的sql查询,例如当我保存新的实体。



我需要它,因为我想保存到日志文件在某些​​情况下。



我的bootstrap.php日志配置:

  Log ',[
'className'=>'File',
'path'=> LOGS.DS.date('Y-m')。DS,
'scopes'= > ['daily','queriesLog'],
'file'=> date('Ym-d'),
]);

我想得到什么:



例如当我保存实体:

  $ this-> Clients-> save 

我想记录一些东西,我想用这个日志保存INSERT sql查询

  Log :: warning('test\\\
',['scope'=> ['daily']]);


解决方案

解决方案可以使用查询记录



http: //book.cakephp.org/3.0/en/orm/database-basics.html#query-logging



您可以在



在我的引导程序中。 php我做了

  Log :: config('current',
[
'className'=> ;'File',
'path'=> LOGS.date('Y-m')DS,//你不需要在LOGS和date()之间的DS
' => ['daily','queriesLog'],
'file'=> date('Ym-d'),
]);

并在我的控制器中

  $ conn = \Cake\Datasource\ConnectionManager :: get('default'); 
$ comment = $ this-> Comments-> get(5620); // 5620是我的一个注释的id
$ conn-> logQueries(true);
$ comment = $ this-> Comments-> get(5619); // 5619是我的一个意见的另一个id
$ comment-> text ='Test';
$ this->留言 - > save($ comment);
$ conn-> logQueries(false);

这样,在logs文件夹中创建一个文件,该文件包含以下

  2015-12-16 13:38:35调试:SELECT ... WHERE Comments.id = 5619 LIMIT 1 
2015- 12-16 13:38:35调试:BEGIN
2015-12-16 13:38:35调试:更新评论SET text ='Test'WHERE id = 5619
2015-12-16 13: 38:35调试:COMMIT

请注意,用于获取注释#5620的查询尚未记录



另请注意,如果您没有启用debugkit,这个方法会有效


How can I view the SQL Query on model->save() in CakePHP 3? Is there any way to do this? I want to get the specific sql query e.g when I save new entity.

I need it because I want to save that to a log file in some cases.

My bootstrap.php log config:

Log::config('current', [
    'className' => 'File',
    'path' => LOGS.DS.date('Y-m').DS,
    'scopes' => ['daily','queriesLog'],
    'file' => date('Y-m-d'),
]);

What i want to get:

e.g when i save entity:

$this->Clients->save($client);

i want to log something and i want to save INSERT sql query with this log

Log::warning('test\n', ['scope' => ['daily']]);

解决方案

A solution could be using query logging

http://book.cakephp.org/3.0/en/orm/database-basics.html#query-logging

you can turn query logging on just when you save your model and then turn it off

For example I have a Comments model

In my bootstrap.php I did

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'), 
]);

and in my controller I did

$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);

In this way a file is created in the logs folder and the file contains the following

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

note that the query used to get comment #5620 has not been logged

Also note that this works if you don't have debugkit enabled

这篇关于如何在CakePHP 3中的model-> save()上获取SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆