A
A
Alexander Nevsky2018-11-25 05:25:36
Yii
Alexander Nevsky, 2018-11-25 05:25:36

How to implement ListView Yii2 filter on related tables?

The connection of the book with the genres goes through the linked table. I display all the books in a ListView and the question arose of filtering books.

$query->joinWith('linkGenresBooks');
        $genres_query = null;
        if(is_array($this->genres_id) and !empty($this->genres_id)){
            foreach($this->genres_id as $k => $g){
                $genres_query[$k] = $q;
            }
        }

        $query->andFilterWhere([
            '{{%linkGenresBooks}}.genres_id' => '3',
        ]);

I tried like this, filtering does not work, besides, because of joinWith, the output of books breaks.
5bfa07bc99d5d217775639.png
Here is the connection:
public function getLinkGenresBooks()
    {
        return $this->hasMany(LinkGenresBooks::className(), ['books_id' => 'id']);
    }

I look in the debug there is no request for genres at all5bfa13f1a9f2f726046661.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nevsky, 2018-11-25
@soy4er

I beg your pardon, I'm crooked, and I spelled the validation badly. Genres were integer, set them to safe and everything is fine. And I found this cant, just starting to print the request itself with print_r, and after checking for validation, it just didn’t pass.
The only question is why 3 out of 18, only 3 entries? This is due to joinWith, but I don’t understand how to fix it5bfa446248b04507803229.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question