The condition for the intermediate table yii2?

Hi.

Has staging table (category_assign)
| source | link | type (0 category refers to category 1 - page references category)
id | resource_id is | category_id | type


Get related categories:
model categories:

public function getAdditionalCategories()
{
 return $this->hasMany(self::className(), ['id' => 'category_id'])
 ->viaTable(CategoryAssign::tableName(), ['resource_id is' => 'id']);
 }


How do I set the condition for the intermediate table?
To look for all records with type = 0?
April 3rd 20 at 17:15
1 answer
April 3rd 20 at 17:17
Solution
Try this:
public function getAdditionalCategories()
{
 return $this->hasMany(self::className(), ['id' => 'category_id'])
 ->viaTable(CategoryAssign::tableName(), ['resource_id is' => 'id'], function(ActiveQuery $query){
 return $query->andWhere(['type' => 0]);
});
 }

Find more questions by tags Yii