Yii調試SQL主要有以下方法:
一、系統自帶調試:
首先index.php開啟調試模式:
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
//app use time
//defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME',microtime(true));
main.php頁面:
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
// 下面顯示頁面日志
array(
'class'=>'CWebLogRoute',
'levels'=>'trace', //級別為trace
'categories'=>'system.db.*' //只顯示關於數據庫信息,包括數據庫連接,數據庫執行語句
),
),
),
YII_TRACE_LEVEL的數字越大,信息越清楚
二、使用調試工具調試:
yii-debug-toolbar把包解壓後 放到extensions裡邊 然後在配置文件main.php中最後加上
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'ext.yii-debug-toolbar.YiiDebugToolbarRoute',
'ipFilters'=>array('127.0.0.1','192.168.1.215'),
),
),
),
沒有出現的話加上在'components'下的db裡加上兩個屬性,
'enableProfiling'=>true, 'enableParamLogging'=>true,
然後如果有其他調試工具的插件的話,可能會出現沖突導致sql語句不出來,把那段代碼注掉即可。
除了cactivedataprovider,你還可以使用CArrayDataProvider
我給你寫個例子:
controller中:
$rawdata = Yii::app()->db->createCommand('select * from {{post}} where id<100')->queryAll();
$data = new CArrayDataProvider($rawdata, array(
'id' => 'user',
'sort' => array(
'attributes' => array(
'id', 'title', 'content'
),
),
'pagination' => array(
'pageSize' => 10,
),
));
$this->render('gridview_array', array('dataProvider' => $data));
然後view中:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'somegrid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'title',
'content',
),
));
這樣就ok 了~
就是一個一個實驗了, 看看哪個結果返回的是你要的。這種自解釋的語言沒有什麼所謂的調試方法。一條命令就搞定的,你認為怎麼算是調試呢