The method of viewing all SQL statements executed by the current page under the YII framework, mainly by configuring related files to achieve the purpose of debugging SQL, as follows:
(1) Modify index.php Open Debug mode
Add the following two lines of code (if not present) in the index.php file:
View Code Printing
2 |
defined( ‘YII_DEBUG‘ ) or define( ‘YII_DEBUG‘ ,true); |
3 |
//设置日志记录级别,YII_TRACE_LEVEL的数字越大,信息越清楚 |
4 |
defined( ‘YII_TRACE_LEVEL‘ ) or define( ‘YII_TRACE_LEVEL‘ ,3); |
(2) Modify the configuration file main.php
Add the following array to the config/main.php components-log-routes:
View Code Printing
2 |
‘class‘ => ‘CFileLogRoute‘ , //文件记录日志的形式 |
3 |
‘levels‘ => ‘trace‘ , //日志记录级别 |
4 |
‘categories‘ => ‘system.db.*‘ , //只显示关于数据库信息,包括数据库连接,数据库执行语句 |
5 |
‘logFile‘ => ‘app_‘ . date ( ‘Y-m-d‘ ). ‘.log‘ , //日志保存文件名 |
6 |
‘logPath‘ => ‘D:\phpStudy\WWW\phpernote\com\tmpfile\log_db‘ , //日志保存路径 |
OK, after the above settings, refresh the page, you can go to the D:\phpStudy\WWW\phpernote\com\tmpfile\log_db directory 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
Yii view (output) The SQL statement executed by the current page