demo函數作用:將AR對象數據轉化為數組
局限:僅用於findAll的多維數組,find一維數組可以先轉化為多維數組的一個元素在使用
1 function actionIndex() 2 { 3 4 $data = Tableone::model()->findAll(array( 5 "limit"=>"10", 6 )); 7 8 /** 9 * ar對象轉化為數組 10 * 僅用於findAll 11 */ 12 $return = array_map(function($record){ 13 $return = $schma = array(); 14 if(isset($record['tableSchema']->columns)) 15 { 16 foreach($record['tableSchema']->columns as $k=>$v) 17 { 18 $schma[] = $k; 19 } 20 } 21 if(count($schma)>0) 22 { 23 foreach($schma as $k=>$v) 24 { 25 $return[$v] = $record->attributes[$v]; 26 } 27 } 28 return $return; 29 },$data); 30 31 print_r($return); 32 33 34 }
除了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 了~
剛回答過。詳見:
zhidao.baidu.com/question/514860097.html?oldq=1