AR 查询出来的 数据,怎么增加额外的属性。 [ 2.0 版本 ]
// 这个方法查询出来的是N条记录
public function actionBonusRecord()
{
$data = UserBonusFlows::find()->where(['uid'=>$this->_uid])
->orderBy("created_at DESC")
->asArray()->all();
$this->appResponse('success', $data);
return true;
}
// 返回的结果
{"code":200,"error":0,"data":[{"id":"84","uid":"325","from_uid":"327","type":"rec","amount":"597.00","ori_amount":"1194.00","created_at":"1553238260","totals":"1791.00","is_incr":"1","remark":null,"sent":"1","sent_time":"1553238260"},{"id":"82","uid":"325","from_uid":"327","type":"rec","amount":"597.00","ori_amount":"597.00","created_at":"1553238078","totals":"1194.00","is_incr":"1","remark":null,"sent":"1","sent_time":"1553238078"},{"id":"80","uid":"325","from_uid":"327","type":"rec","amount":"597.00","ori_amount":"0.00","created_at":"1553237952","totals":"597.00","is_incr":"1","remark":null,"sent":"1","sent_time":"1553237952"}],"message":""}
比如我现在想在返回的结果 的每一条记录里的 type 属性 根据它的值不同 显示不同的汉字, 可能有很多条数据 不想循环赋值,该肿么办?
沃斯尼达耶 补充于 2019-04-09 10:09
还有 比如数据表里是没有 count 属性的, 我怎么才能在每一条查询结果里 加上count属性 并且计算出结果赋值给count属性。
沃斯尼达耶 补充于 2019-04-09 10:12
在 AR 里使用 getXX
方法 $model->xx
时才有值, 现在就是想查询出来的数据 能加入属性。
沃斯尼达耶 补充于 2019-04-09 10:24
用了这个方法
public function attributes ()
{
$attributes = parent::attributes();
$attributes[] = 'count';
$attributes[] = 'name';
return $attributes;
}
是多了两个属性 但是查询出来的结果 还是没有他俩, 是需要在哪里给这俩赋值么?
$attributes =
Array
(
[0] => id
[1] => uid
[2] => from_uid
[3] => type
[4] => amount
[5] => ori_amount
[6] => created_at
[7] => totals
[8] => is_incr
[9] => remark
[10] => sent
[11] => sent_time
[12] => count
[13] => name
)
沃斯尼达耶 补充于 2019-04-11 17:07
这两个方法都能实现 但是就这个问题来说 感觉2 反倒是麻烦了呢。
1.
$list = User::find()->where(['status' => 0])->all();
foreach ($list as &$v) {
$v->type = 1;
}
var_dump($list);exit;
2.
$query = Industry::find()->select(['id', 'name', 'description', 'sort', 'updated_at'])
->where(['status' => Industry::STATUS_ACTIVE])
->orderBy(['sort' => SORT_DESC, 'id' => SORT_ASC]);
$industries = ArrayHelper::toArray($query->all(), [Industry::class => [
'id', 'name', 'description', 'sort',
'updated_date' => function($model){
return Helper::formatTime($model->updated_at);
},
]]);
最佳答案
其他 6 个回答
-
// Yii2 active_record 添加额外属性 public function attributes() { $attributes = parent::attributes(); $attributes[] = 'type'; return $attributes; }
共 1 条回复谢谢, 用了这个办法 还是没有
public function attributes () { $attributes = parent::attributes(); $attributes[] = 'count'; $attributes[] = 'name'; return $attributes; }
{"code":200,"error":0,"data":[{"id":"84","uid":325,"from_uid":327,"type":"rec","amount":"597.00","ori_amount":"1194.00","created_at":1553238260,"totals":"1791.00","is_incr":1,"remark":null,"sent":1,"sent_time":1553238260},{"id":"82","uid":325,"from_uid":327,"type":"rec","amount":"597.00","ori_amount":"597.00","created_at":1553238078,"totals":"1194.00","is_incr":1,"remark":null,"sent":1,"sent_time":1553238078},{"id":"80","uid":325,"from_uid":327,"type":"rec","amount":"597.00","ori_amount":"0.00","created_at":1553237952,"totals":"597.00","is_incr":1,"remark":null,"sent":1,"sent_time":1553237952}],"message":""}
-
public function fields() { return [ ……你的已有属性或字段, 'count' => function() { return $this->已有属性1 + $this->已有属性2; } ]; } // 假设该 AR 类为 Order, $order = new Order(); // 访问 `count` 可以使用: $order->count;
参考链接:https://www.yiichina.com/doc/api/2.0/yii-base-arrayabletrait#fields()-detail
-
用
ArrayHelper::toArray()
方法$query = Industry::find()->select(['id', 'name', 'description', 'sort', 'updated_at']) ->where(['status' => Industry::STATUS_ACTIVE]) ->orderBy(['sort' => SORT_DESC, 'id' => SORT_ASC]); $industries = ArrayHelper::toArray($query->all(), [Industry::class => [ 'id', 'name', 'description', 'sort', 'updated_date' => function($model){ return Helper::formatTime($model->updated_at); }, ]]);
沃斯尼达耶 觉得很赞
沃斯尼达耶 秦皇岛
注册时间:2016-11-02
最后登录:2022-11-03
在线时长:34小时55分
最后登录:2022-11-03
在线时长:34小时55分
- 粉丝8
- 金钱2060
- 威望0
- 积分2400