E
E
EVOSandru62017-09-13 14:05:42
Yii
EVOSandru6, 2017-09-13 14:05:42

What is the correct way to perform grouping in ActiveRecord for a query with multiple LEFT JOINs?

Good afternoon,
There are such models with links:
class Offers (offers)

public function getDeals()
    {
        return $this->hasMany(Deals::className(), ['offer_id' => 'id']);
    }
    public function getBrand()
    {
        return $this->hasOne(Brands::className(), ['id' => 'brand_id']);
    }
    public function getCategory()
    {
        return $this->hasOne(CategoryOffers::className(), ['id' => 'category_id']);
    }

class Deals (products - several per offer)
public function getCoupons()
    {
        return $this->hasMany(Coupons::className(), ['deal_id' => 'id']);
    }

    public function getOffer()
    {
        return $this->hasOne(Offers::className(), ['id' => 'offer_id']);
    }

class CategoryOffers ( offer category )
public function getOffers()
    {
        return $this->hasMany(Offers::className(), ['category_id' => 'id']);
    }

class Coupons ( coupons - analogy of standard Orders - only 1 coupon - 1 item )
public function getDeal()
    {
        return $this->hasOne(Deals::className(), ['id' => 'deal_id']);
    }

I need to pull 5 brands by top selling products of the current category:
CategoryOffers::getHotBrands
$brands = Brands::find()->select(['m_brands.*','count(m_coupons.deal_id) as cnt'])
            ->joinWith(['offers'=>function($q) {
                  $q->joinWith(['deals'=>function($q){
                    $q->joinWith(['coupons'=>function($q) {
                         $q
                            ->orderBy('cnt desc')
                             ->limit(5)
                            ->groupBy('m_coupons.deal_id')
                            ;
                    }]);
                }])->groupBy(['m_offers.brand_id'])
                ;
            }])->all();
}

SQLSTATE[42803]: Grouping error: 7 ERROR: column "m_brands.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "m_brands".*, count(m_coupons.*) as cnt FROM "m_brand...
^
The SQL being executed was: SELECT "m_brands".*, count(m_coupons.*) as cnt FROM "m_brands" LEFT JOIN "m_offers" ON "m_brands"."id" = "m_offers."" brand_id" LEFT JOIN "m_deals" ON "m_offers"."id" = "m_deals"."offer_id" LEFT JOIN "m_coupons" ON "m_deals"."id" = "m_coupons"."deal_id" GROUP BY "m_offers". "brand_id", "m_coupons"."deal_id" ORDER BY "cnt"DESC
Error Info: Array
(
[0] => 42803
[1] => 7
[2] => ERROR: column "m_brands.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "m_brands".*, count(m_coupons.*) as cnt FROM "m_brand... Can

you please tell me what I need to change to make it work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Musin, 2017-09-21
@vmpartner

This is not done through ActiveRecord, I would not accept such code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question