Answer the question
In order to leave comments, you need to log in
How to make a selection of similar products in Yii2?
There are 3 linked tables using 2 linking tables.
The books table is associated with the genres table, just like books is associated with tags
The relationship is normal
public function getLinkGenresBooks()
{
return $this->hasMany(LinkGenresBooks::className(), ['books_id' => 'id']);
}
public function getGenres()
{
return $this->hasMany(Genres::className(), ['id' => 'genres_id'])->via('linkGenresBooks');
}
public function getLinkTagsBooks()
{
return $this->hasMany(LinkTagsBooks::className(), ['books_id' => 'id']);
}
public function getTags()
{
return $this->hasMany(Tags::className(), ['id' => 'tags_id'])->via('linkTagsBooks');
}
$test = Books::find()->joinWith('genres g')->joinWith('tags t')->where(['or', ['g.id' => [1, 21, 30]]])->andWhere(['or', ['t.id' => [1, 5]]])->all();
Answer the question
In order to leave comments, you need to log in
Since your connection is HasMany, the number will fail. It is necessary either to use GROUP BY or to receive in several requests, choosing id-shniki.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question