Answer the question
In order to leave comments, you need to log in
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',
]);
public function getLinkGenresBooks()
{
return $this->hasMany(LinkGenresBooks::className(), ['books_id' => 'id']);
}
Answer the question
In order to leave comments, you need to log in
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 it
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question