使用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

如果返回的是多条结果,返回的是一个对象数组的时候有下面3种方法:

//第一种直接将结果循环输出
$trips = Trips::model()->findAll();
$arr = array();
foreach($trips as $t)
{
$arr[$t->id] = $t->attributes;
}
//第二种用array_map
$result= array_map(function($record) { return $record->attributes;}, Post::model()->findAllByAttributes($attributes));

或者重写findAll方法:

/**
* 重写findALL方法
* @params $condition 查询条件,$params 参数,$return_array 是否返回数组
*
* return 根据条件返回结果类型
*/
public function findAll($condition = '',$params=array(), $return_array=false)
{
$result = CActiveRecord::findAll($condition,$params);
if($return_array)
{
$result = array_map(create_function('$record','return $record->attributes;'),$this->findAll($condition,$params));
}
return $result;
}
//第三种方法
$array = CJSON::decode(CJSON::encode($model));

或者使用DAO

Use DAO for arrays

$array =Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll();

其它参考:http://stackoverflow.com/questions/4435886/yii-model-to-array

Yii Active Record 查询结果转化成数组的更多相关文章

  1. [YII]将ar查询结果转化成数组

    $Column= Column::model()->findAll(); $data = array_map(create_function('$m', 'return $m->getAt ...

  2. xml格式的数据转化成数组

    将得到的xml格式的数据转化成数组 <?php //构造xml $url = "http://api.map.baidu.com/telematics/v3/weather?locat ...

  3. yii2得到的数据对象转化成数组

    yii2得到的数据对象转化成数组需要用到asArray().1.Customer::find(['id' => $id])->asArray()->one();2.$model = ...

  4. laravel 将数组转化成字符串 再把字符串转化成数组

    这是在给阮少翔改代码的时候用的方法, 开始的数据用explored转化成数组不是想要的结果, 我就自己写了一个方法把有用的信息提取出来拼接成一个字符串, 再用explored将字符串转化成数组.   ...

  5. 实现类数组转化成数组(DOM 操作获得的返回元素值是一个类数组)

    目标 实现类数组转化成数组 实例 链接地址 使用方法 const foo = document.querySelectorAll('.result') //链接地址输入控制台输入这行代码 const ...

  6. Yii 将对象转化成数组

    将从数据库查找的对象,转换成数组,并且以设定属性键名称,用到ArrayHelper::toArray $posts = Post::find()->limit(10)->all(); $d ...

  7. Yii框架AR对象数据转化为数组

    demo函数作用:将AR对象数据转化为数组 局限:仅用于findAll的多维数组,find一维数组可以先转化为多维数组的一个元素在使用 function actionIndex() { $data = ...

  8. 将类数组arguments转化成数组

    一.将arguments对象绑定到slice方法上 二. 例子: //闭包 二次封装函数 求和 function partialUsingArguments(fn) { var args = Arra ...

  9. sqlachemy查询对象转化成字典/json使用

    https://www.cnblogs.com/sanduzxcvbnm/p/10220718.html

随机推荐

  1. vs2013 上传碰到的问题:“输入的不是有效的 Base-64 字符串 ”

    action 代码: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create( ImageStoreModels images ...

  2. 我的PHP之旅--PHP的函数初步认识

    函数 函数主要是将一块代码封装起来方便多次使用,方便以后维护,节省代码. 先看一个简单的函数: <?php function myFirstFunc(){ echo "Hello PH ...

  3. Hex、bin、axf、elf格式文件小结

    转自Hex.bin.axf.elf格式文件小结 一.HEX Hex文件,一般是指Intel标准的十六进制文件.Intelhex 文件常用来保存单片机或其他处理器的目标程序代码.它保存物理程序存储区中的 ...

  4. 静态分析安全测试(SAST)优缺点探析

    静态分析安全测试(SAST)是指不运行被测程序本身,仅通过分析或者检查源程序的语法.结构.过程.接口等来检查程序的正确性,那么采用静分析安全测试的方法有什么优缺点呢,且让小编给你说道说道. 许多公司都 ...

  5. POJ 3352 Road Construction (边双连通分量)

    题目链接 题意 :有一个景点要修路,但是有些景点只有一条路可达,若是修路的话则有些景点就到不了,所以要临时搭一些路,以保证无论哪条路在修都能让游客到达任何一个景点 思路 :把景点看成点,路看成边,看要 ...

  6. table 表头固定

    <html> <head> <title>Test</title> <style type="text/css"> .d ...

  7. c/c++ 重载 数组 操作符[] operator[ is ambiguous, as 0 also mean a null pointer of const char* type.

    // Note: //int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of ...

  8. ptr_fun学习笔记

    ptr_fun是将一个普通的函数适配成一个functor,添加上argument type和result type等类型, 其实现如下(例子里面是binary_function,unary_funct ...

  9. BZOJ_2002_弹飞绵羊_(LCT)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2002 一列n个数,a[i]表示向后a[i]个,问第k个数进行多少次向后跳跃会飞出去. 分析 i ...

  10. [POJ 1155] TELE

    TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3787   Accepted: 2007 Description ...