Yii调试SQL的常用方法

jerry Yii 2015年08月23日 收藏

Yii调试SQL主要有以下方法:
一、系统自带调试:
首先index.php开启调试模式:

  1. // remove the following lines when in production mode 
  2. defined('YII_DEBUG') or define('YII_DEBUG',true); 
  3. // specify how many levels of call stack should be shown in each log message 
  4. defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3); 
  5. //app use time 
  6. //defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME',microtime(true));
  7. main.php页面:
  8. 'errorHandler'=>array( 
  9.   // use 'site/error' action to display errors 
  10.   'errorAction'=>'site/error', 
  11. ),
  1. 'log'=>array( 
  2.   'class'=>'CLogRouter', 
  3.   'routes'=>array( 
  4.     array( 
  5.       'class'=>'CFileLogRoute', 
  6.       'levels'=>'error, warning', 
  7.     ), 
  8.     // 下面显示页面日志 
  9.     array( 
  10.       'class'=>'CWebLogRoute', 
  11.       'levels'=>'trace',   //级别为trace 
  12.       'categories'=>'system.db.*' //只显示关于数据库信息,包括数据库连接,数据库执行语句 
  13.     ),    
  14.   ), 
  15. ),

YII_TRACE_LEVEL的数字越大,信息越清楚
二、使用调试工具调试:
yii-debug-toolbar把包解压后 放到extensions里边 然后在配置文件main.php中最后加上

  1. 'log'=>array( 
  2.    'class'=>'CLogRouter', 
  3.    'routes'=>array( 
  4.      array( 
  5.        'class'=>'ext.yii-debug-toolbar.YiiDebugToolbarRoute', 
  6.        'ipFilters'=>array('127.0.0.1','192.168.1.215'), 
  7.      ), 
  8.    ), 
  9.  ),

没有出现的话加上在'components'下的db里加上两个属性,

  1. 'enableProfiling'=>true, 
  2. 'enableParamLogging'=>true,

然后如果有其他调试工具的插件的话,可能会出现冲突导致sql语句不出来,把那段代码注掉即可。