D
D
Don_Malus2020-11-30 20:26:12
Yii
Don_Malus, 2020-11-30 20:26:12

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

1 answer(s)
K
Kulay, 2020-12-04
@Don_Malus

public function getArticles()
{
return $this->hasMany(Article::className(), ['id' => 'article_id'])
->viaTable('article_tag', ['tag_id' => 'id']);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question