This article shares how to view all sql statements executed on the current page under the Yii framework, mainly by configuring related files To achieve the purpose of debugging sql, the specific method is as follows:
(1) Modify index.php to enable debugging mode
Add the following two lines of code in the index.php file (if it does not exist):
//开启调试模式 defined('YII_DEBUG') or define('YII_DEBUG',true); //设置日志记录级别,YII_TRACE_LEVEL的数字越大,信息越清楚 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
(2) Modify the configuration file main.php
Add the following array under components - log - routes in config/main.php:
array( 'class'=>'CFileLogRoute',//文件记录日志的形式 'levels'=>'trace',//日志记录级别 'categories'=>'system.db.*',//只显示关于数据库信息,包括数据库连接,数据库执行语句 'logFile' => 'app_'.date('Y-m-d').'.log',//日志保存文件名 'logPath'=>'D:\phpStudy\WWW\phpernote\com\tmpfile\log_db',//日志保存路径 ),
ok, after the above settings, refresh the page, you can go to the directory D:phpStudyWWWphpernotecomtmpfilelog_db to find the generated sql record file. The output log format is as follows:
[Time] - [Level] - [Category] - [Content]
2015/04/17 10:30:51 [trace] [system.db.CDbCommand] Querying SQL: SELECT * FROM `phpernote_article` ORDER BY id desc LIMIT 15