Добрый день,
Есть такие модели со связями:
class 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 (товары - по нескольку на одно предложение)
public function getCoupons()
{
return $this->hasMany(Coupons::className(), ['deal_id' => 'id']);
}
public function getOffer()
{
return $this->hasOne(Offers::className(), ['id' => 'offer_id']);
}
class CategoryOffers ( категория предложения )
public function getOffers()
{
return $this->hasMany(Offers::className(), ['category_id' => 'id']);
}
class Coupons ( купоны - аналогия стандартного Orders - только 1купон - 1товар )
public function getDeal()
{
return $this->hasOne(Deals::className(), ['id' => 'deal_id']);
}
Мне нужно вытащить 5 брендов по самым продаваемым товарам текущей категории:
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...
Подскажите пожалуйста, что мне нужно изменить, чтобы заработало?