VictoryForYou VictoryForYou - 2 months ago 33
PHP Question

Yii2 output two column values of sum()

$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');


I need to
sum(['cards.card_value','orders.price'])
, but its param is a string not an array. How can I do it?

Answer

You can place the two sum in select using leteral format

$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]);