首页 > 编程语言 > 详细

Yii Active Record 查询结果转化成数组

时间:2015-07-31 18:03:34      阅读:475      评论:0      收藏:0      [点我收藏+]

使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回。比如下面的方法:

// 查找满足指定条件的结果中的第一行
$post=Post::model()->find($condition,$params);
// 查找具有指定主键值的那一行
$post=Post::model()->findByPk($postID,$condition,$params);
// 查找具有指定属性值的行
$post=Post::model()->findByAttributes($attributes,$condition,$params);

返回一条结果的时候直接用 $post->attributes; 就可以了。

Post::model()->find()->attributes
如果返回的是多条结果,返回的是一个对象数组的时候有下面2种方法:
//第一种直接将结果循环输出
 foreach ($myReceivedCode as $model) {
                        $result[] = $model->attributes;
                }

//第二种用array_map
                $result= array_map(function($record) {
                                return $record->attributes;
                        }, Post::model()->->findAllByAttributes($attributes));

Yii Active Record 查询结果转化成数组

原文:http://www.cnblogs.com/DaBing0806/p/4692576.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!