2019-04-09 10:07:44 2014次浏览 7条回答 0 悬赏 188 金钱
// 这个方法查询出来的是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);
},

]]);

最佳答案

  • marslu 发布于 2019-04-09 10:42 举报
    $list = User::find()->where(['status' => 0])->all();
    foreach ($list as &$v) {
        $v->type = 1;
    }
    var_dump($list);exit;
    
  • 回答于 2019-04-09 10:11 举报
    // Yii2 active_record 添加额外属性
    public function attributes()
    {
        $attributes = parent::attributes();
        $attributes[] = 'type';
        return $attributes;
    }
    
    1 条回复
    回复于 2019-04-09 10:16 回复

    谢谢, 用了这个办法 还是没有

    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":""}
    
  • 回答于 2019-04-09 10:14 举报

    AR是啥意思啊?

    1 条回复
    回复于 2019-04-09 10:20 回复

    ActiveRecord

  • 回答于 2019-04-09 10:45 举报
    class UserBonusFlows extends ActiveRecord{
        public typeName='';
    }
    

    这样你试试

  • 回答于 2019-04-09 10:53 举报
    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

  • 回答于 2019-04-09 14:44 举报

    代码的世界

  • 回答于 2019-04-09 15:15 举报

    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分
  • 粉丝8
  • 金钱2060
  • 威望0
  • 积分2400

热门问题