A
A
Alexander Nevsky2018-11-01 15:04:06
Yii
Alexander Nevsky, 2018-11-01 15:04:06

How to make yii2 filters correctly?

I would like to make a selection of books by genre. That is, after selecting two genres, books would be displayed with one genre and with the second and with two genres together (well, the same thing with a larger number). I can't figure out exactly how to make a request.

$query->andFilterWhere(['like', 'genres', $this->genres])
            ->andFilterWhere(['like', 'tags', $this->tags])
            ->andFilterWhere(['like', 'joint_authors', $this->joint_authors])
            ->andFilterWhere(['like', 'publishing', $this->publishing]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-11-01
@soy4er

You will have an array in the filter, which you will then run through foreach and form the correct query, such as:

if(is_array($this->genres) and !empty($this->genres)){
    $genres_query = ['or'];
    foreach($this->genres as $one){
        $genres_query[] = ['like', 'genres', $one];
    }
}
$query->andFilterWhere($genres_query)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question