Answer the question
In order to leave comments, you need to log in
The relationship has many through the third table. In one direction I can pull out the information, but not in the other. Why?
Classic: Articles and tags linked through a third table.
$article->tags shows everything without problems, but $tag->articles gives either zero or an error: Getting unknown property: yii\db\ActiveQuery::articles
Link from Tags model:
public function getArticles()
{
return $this ->hasMany(Article::className(), ['article_id' => 'id'])
->viaTable('article_tag', ['id' => 'tag_id']);
}
Communication from the Articles model
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('article_tag', ['article_id' => 'id']);
public function actionSearchtag()
{
// Parse the request
$search = Yii::$app->request->get('search');
// Trim spaces
$search = str_replace(' ', '', $search);
// Search query with finding and trimming spaces
$tag = Tag::find()->where(['like', 'replace(title, " ", "")', $search]);
$articles = $tag->articles; // swears at this line
...
}
I've studied the documentation on links, it says everywhere that such a dependency is enough for such an appeal. I don’t understand, I’m a fool or the skis don’t go. Thanks in advance.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question