Yii框架AR对象数据转化为数组,yiiar_PHP教程
Yii框架AR对象数据转化为数组,yiiar
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 idqueryAll();
$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