如何在yii框架中向数据库显示所有查询

如何在yii框架中向数据库显示所有查询,yii,Yii,在CodeIgniter中,我会: print_r ($this->db->queries); 在Yii中,我尝试: print_r (Yii::app()->db) 但这并没有显示任何查询 更新: 我理解我的问题:当我想在POST操作上显示db查询时,我不显示它。当使用GET时,没关系。正如@bool.dev所说,您可以使用CWebLogRoute或者在我的例子中,我使用CFileLogRoute将这些查询存储在文件中 array ( 'class'

在CodeIgniter中,我会:

print_r ($this->db->queries);
在Yii中,我尝试:

 print_r (Yii::app()->db)
但这并没有显示任何查询

更新:
我理解我的问题:当我想在
POST
操作上显示db查询时,我不显示它。当使用
GET
时,没关系。

正如@bool.dev所说,您可以使用
CWebLogRoute
或者在我的例子中,我使用
CFileLogRoute
将这些查询存储在文件中

array (
    'class'      => 'CFileLogRoute',
    'categories' => 'system.db.*',
    'logFile'    => 'sql.log',
),

为了补充@snippLeaf com的答案,您可以通过以下关键字跟踪此文件过滤:

// filter by "INSERT" or "UPDATE"
$ tail -f /path_to/protected/runtime/sql.log |grep 'INSERT\|UPDATE'

// filter (case insensitive) by "SELECT" in table "x2_users"
$ tail -f /path_to/protected/runtime/sql.log |grep -i SELECT.*x2_users
OBS:要获取新数据,可能需要刷新数据库缓存:

rm -f protected/runtime/cache/*.bin

如果确实希望每个查询都登录到yii中,请使用扩展

步骤1。从-->

步骤2。解包到
受保护/扩展/

步骤3。将文件夹名
yii db profiler master
重命名为
db\u profiler

步骤4。将以下内容更新到受保护的
protected/config/main.php

<?php
return array(
    // …
    'components' => array(
        // …
        'db' => array(
            // …
            'enableProfiling'=>true,
            'enableParamLogging' => true,
        ),
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                    // …
                    array(
                        'class'=>'ext.db_profiler.DbProfileLogRoute',
                        'countLimit' => 1, // How many times the same query should be executed to be considered inefficient
                        'slowQueryMin' => 0.01, // Minimum time for the query to be slow
                    ),
            ),
        ),
    ),
);

你也使用了CWebLogRoute吗?对我来说CWebLogRoute更好。非常感谢。