开发者

How to use yii2 joinWith aggregation (count, sum) query

I know use with query:

$customers = Customer::find()->with([
    'country',
    'orders' => function ($query) {
        $query->andWhere(['status' => Order::STATUS_ACTIVE]);
    },
])->all();

But, When I use count in with query, The result didn't contains aggregation column, Like that:

$query = UserInviteRecord::find()
    ->alias('ui')
    ->with([
        'userInfo',
        'inviteOrder' => function ($query) {
            $query->select(['invite_user_id', 'count(id) as total_count']);
            $query->where(['status' => 1]);
            $query->groupBy('invite_user_id');
        }])
    ->where(['ui.type' => 0])

Result only contains invite_user_id column.

How to use yii2 joinWith aggregation (count, sum) query

replace select query:

$query->select(['invite_user_id', 'count(id) as开发者_Go百科 total_count']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜