J
J
JohnxFFF2019-12-10 09:58:23
Yii
JohnxFFF, 2019-12-10 09:58:23

Condition for intermediate table yii2?

Hello.
There is an intermediate table ( category_assign )

|  источник   |    ссылка      |  тип (0 категория ссылается на категорию, 1 - страница ссылается на категорию)
id | resource_id | category_id  | type

I get related categories:
модель категории:

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

How do I set a condition for an intermediate table?
To search for all records with type = 0?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-12-10
@JohnxFFF

Try like this:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question