开发者

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

开发者 https://www.devze.com 2022-12-07 19:11 出处:网络
I know use with query: $customers = Customer::find()->with([ \'country\', \'orders\' => function ($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

精彩评论

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

关注公众号