Answer the question
In order to leave comments, you need to log in
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']);
}
public function getCoupons()
{
return $this->hasMany(Coupons::className(), ['deal_id' => 'id']);
}
public function getOffer()
{
return $this->hasOne(Offers::className(), ['id' => 'offer_id']);
}
public function getOffers()
{
return $this->hasMany(Offers::className(), ['category_id' => 'id']);
}
public function getDeal()
{
return $this->hasOne(Deals::className(), ['id' => 'deal_id']);
}
$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();
}
Answer the question
In order to leave comments, you need to log in
This is not done through ActiveRecord, I would not accept such code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question