R
R
rasul2662020-12-20 00:49:56
Yii
rasul266, 2020-12-20 00:49:56

How to implement multi-attribute search in Yii2 EAV model?

Search by one attribute turns out everything works, but for several attributes I can’t catch up with how to do a search ((
my code

$productsQuery = Product::find()->joinWith('values')
                                ->andWhere(['product.category_id' => $category->id])
                                ->andWhere(['like', 'value.value', '%'.$size])
                               ->andWhere(['value.attribute_id'=>$sizeAttr['id']])->all();


here products are selected by 'size' but how to select by size by color, etc. by several attributes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Rybin, 2020-12-20
@RybinDen

The general scheme is to create a search model class and, depending on the GET parameters received, connect the appropriate filters to the $query object in it.
In your question instead of andWhere use andFilterWhere
https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question