Yii2输出sum()

时间:2016-09-24 02:21:41

标签: php yii2

$queryOrigin = Order::find()
            ->andFilterWhere(['orders.id' => $r->get('id')])
            ->andFilterWhere(['orders.type' => $r->get('type')])
            ->andFilterWhere(['orders.phone_num' => $r->get('phone_num')])
            ->andFilterWhere(['orders.channel_id' => $r->get('channel_id')])
            ->andFilterWhere(['orders.channel_order_id' => $r->get('channel_order_id')])
            ->andFilterWhere(['orders.fail_reason' => $r->get('fail_reason')])
            ->andFilterWhere(['>=', 'orders.created_at', $start_time])
            ->andFilterWhere(['<=', 'orders.created_at', $end_time])
            ->andFilterWhere(['orders.type' => 1])
            ->andFilterWhere(['orders.status' => $r->get('status')])
            ->andFilterWhere(['like', 'orders.area', empty($row)?null:$row[0]['area_name']])
            ->andFilterWhere(['like', 'orders.operator', $r->get('operator')])
            ->joinWith(['provider'])->andFilterWhere(['cards.provider_id' => $provider,'order_card_items.flow_number' => $flow_num])
            ->sum('orders.price');

我需要sum(['cards.card_value','orders.price']),但它的参数是字符串而不是数组。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用leteral format

将两个sum放入select中
$queryOrigin = Order::find()->select(' sum(orders.price), sum(cards.card_value) ')
        ->andFilterWhere(['orders.id' => $r->get('id')])
        ->andFilterWhere(['orders.type' => $r->get('type')])
        ->andFilterWhere(['orders.phone_num' => $r->get('phone_num')])
        ->andFilterWhere(['orders.channel_id' => $r->get('channel_id')])
        ->andFilterWhere(['orders.channel_order_id' => $r->get('channel_order_id')])
        ->andFilterWhere(['orders.fail_reason' => $r->get('fail_reason')])
        ->andFilterWhere(['>=', 'orders.created_at', $start_time])
        ->andFilterWhere(['<=', 'orders.created_at', $end_time])
        ->andFilterWhere(['orders.type' => 1])
        ->andFilterWhere(['orders.status' => $r->get('status')])
        ->andFilterWhere(['like', 'orders.area', empty($row)?null:$row[0]['area_name']])
        ->andFilterWhere(['like', 'orders.operator', $r->get('operator')])
        ->joinWith(['provider'])->andFilterWhere(['cards.provider_id' => $provider,'order_card_items.flow_number' => $flow_num]);