yii AR对象转换为数组

通过find findAll等查询出来的结果为CActiveRecord对象,有时候需要当做数组来处理

$testArObj = Test::model()->findAll();
$stuList = array();
if (!empty($testArObj)) {
    $stuList = json_decode(CJSON::encode($testArObj),true);
 }

// stuList现在就是数组  findAll查询结果返回空数组,find没有结果返回null

yii1中有时候会用到CActiveDataProvider对象和挂件widget来渲染末班,我们也可以把CActiveDataProvider对象转换为数组,方便调试。

// $this 就是当前的数据模型
$res = new CActiveDataProvider($this, array(
     'criteria'=>$criteria,
));
$stuList = json_decode(CJSON::encode($res->getData()),true);
print_r($stuList);die;
// ↑ stuList现在就是数组

 

posted @ 2016-12-26 13:13  骑着蜗牛的蚂蚁  阅读(2438)  评论(0编辑  收藏  举报