Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取模型上的SQL查询->;在CakePHP 3中保存()吗?_Php_Cakephp_Cakephp 3.0 - Fatal编程技术网

如何获取模型上的SQL查询->;在CakePHP 3中保存()吗?

如何获取模型上的SQL查询->;在CakePHP 3中保存()吗?,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,如何在CakePHP 3中查看model->save()上的SQL查询?有没有办法做到这一点?我想获取特定的sql查询,例如,当我保存新实体时 我需要它,因为我想在某些情况下将其保存到日志文件中 My bootstrap.php日志配置: Log::config('current', [ 'className' => 'File', 'path' => LOGS.DS.date('Y-m').DS, 'scopes' => ['daily','queri

如何在CakePHP 3中查看model->save()上的SQL查询?有没有办法做到这一点?我想获取特定的sql查询,例如,当我保存新实体时

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

My bootstrap.php日志配置:

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

e、 g当我保存实体时:

$this->Clients->save($client);
我想记录一些东西,我想用这个日志保存INSERT sql查询

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

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

您可以在保存模型时打开查询日志记录,然后将其关闭

例如,我有一个评论模型

在我的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'), 
]);
在我的控制器里我做到了

$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);
通过这种方式,将在logs文件夹中创建一个文件,该文件包含以下内容

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
请注意,用于获取注释#5620的查询尚未记录

还要注意的是,如果您没有启用debugkit,则可以将其放入模型中

函数getLastQuery(){ Configure::write('debug',false); $dbo=$this->getDatasource(); $logs=$dbo->getLog(); $lastLog=end($logs['log']); $latQuery=$lastLog['query']; 回声“ 调用该函数

$this->getLastQuery();
并回应它


试试看。

是的,我看到了这个解决方案,但它保存了页面呈现时创建的整个sql。但我需要为某些操作获取特定查询,而不仅仅是在调试模式下。我需要类似以下内容$log=$this->Model->getDataSource()->getLog(false,false);这是在Cakephp 2中,您可以在调用model->save之前打开日志记录。当调试模式为offI时,它会工作。我在model->save()之前将查询日志记录设置为true,但这不起任何作用。我的配置如下:Log::config('current',['className'=>'File','path'=>LOGS.DS.date('Y-m').DS',scopes'=>['daily','queriesLog'],'file'=>date('Y-m-d'),];我测试了你的代码,它运行正常(它实际创建了包含日期和日志文件的文件夹)。我还注意到,如果启用了debugKit,它将捕获查询,因此不会向记录器发送任何内容。是否要查看在保存新实体之前执行的查询?我想查看模型->保存()时插入和更新操作的查询。我已经发布了一个答案,请检查它,它是否是必需的?这不是cake 2。*?getDatasource()和getLog()函数在cakephp 3中不存在,对吗?它不适用于您的代码我得到了未定义的方法getDatasource(),但我编辑了您的代码,使之成为cakephp 3就绪$dbo=ConnectionManager::get('default'))$logs=$dbo->getLog();$lastLog=end($logs['log']);$latQuery=$lastLog['query'];echo”“;print_r($latQuery);但现在它说“调用未定义的方法Cake\Database\Connection::getLog()”cakephp3:最简单的方法是打印$model->sql()
after $this->save($data);