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

sqserrrh  于 2022-11-09  发布在  其他
关注(0)|答案(3)|浏览(164)

在CodeIgniter中,我将执行以下操作:

print_r ($this->db->queries);

在Yii我尝试:

print_r (Yii::app()->db)

但这并没有显示任何查询。
更新:我了解我的问题:当我想在POST操作上显示数据库查询时,我没有显示它。当使用GET时,它是可以的。

qvtsj1bj

qvtsj1bj1#

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

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

yqhsw0fo2#

为了补充@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
lrpiutwd

lrpiutwd3#

如果你真的希望每个查询日志都在yii中使用yii db profiler扩展名。

步骤1.从--〉link下载扩展
步骤2.打开 Package 至protected/extensions/
步骤3.将文件夹名称yii-db-profiler-master重命名为db_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
                    ),
            ),
        ),
    ),
);

相关问题