V
V
vnpp2018-03-19 17:04:10
Yii
vnpp, 2018-03-19 17:04:10

How to filter data by intermediate link table (hasMany/hasOne with via/viaTable) in Yii2 model?

there is a link according to the scheme Buyer - Sales - Supplier, i.e. The Buyer is connected to suppliers through Sales
for the getter in the Buyer model, we have

return $this->hasMany(ПОСТАВЩИКИ::className(), ['ПОСТАВЩИК_ID' => 'ПРОДАЖИ_ID_ПОСТАВЩИКА'])
->viaTable('ПРОДАЖИ', ['ПРОДАЖИ_ID_ПОКУПАТЕЛЯ' => 'ПОКУПАТЕЛЬ_ID'])

Some sales have been canceled or have not taken place yet. in the sales table, records are marked as inactive.
Adding where/andWhere to fields in the Sales table does not work.
It is possible to join the Sales table and then filter, but it looks like a crutch.
How to filter data by intermediate link table (hasMany/hasOne with via/viaTable) in Yii2 model?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
davidnum95, 2018-03-19
@vnpp

return $this->hasMany(ПОСТАВЩИКИ::className(), ['ПОСТАВЩИК_ID' => 'ПРОДАЖИ_ID_ПОСТАВЩИКА'])
         ->viaTable(
              'ПРОДАЖИ', 
              ['ПРОДАЖИ_ID_ПОКУПАТЕЛЯ' => 'ПОКУПАТЕЛЬ_ID'], 
              function ($query) {
                    $query->andWhere(['СТАТУС_ПРОДАЖИ' => 'АКТИВНА']);
              });

V
Viktor Yanyshev, 2018-03-19
@villiwalla

return $this->hasMany(ПОСТАВЩИКИ::className(), ['ПОСТАВЩИК_ID' => 'ПРОДАЖИ_ID_ПОСТАВЩИКА'])
->viaTable('ПРОДАЖИ', ['ПРОДАЖИ_ID_ПОКУПАТЕЛЯ' => 'ПОКУПАТЕЛЬ_ID'])->where(['table_name.columns'  => value])

V
vnpp, 2018-06-02
@vnpp

The most flexible option is to perform all JOINs on your own and filter by any fields of intermediate tables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question