M
M
Mikhail Vasiltsev2018-12-05 16:15:27
Yii
Mikhail Vasiltsev, 2018-12-05 16:15:27

How to get link links in YII2?

I am doing a project purely for myself, for learning. I implement tags for posts. a separate table of posts, a separate label and a separate table of links between posts and labels.
What do I do next. When I receive a specific post, I get it with it through with links to tags. But since the link table does not contain label names, but contains only post_id , label_id , then I still need to reach the label names, which are respectively in a separate label table, in order to display them in a graphical representation when displaying posts. I don't understand how to do this
Current request

$posts = Posts::find()->limit(30)->with('labelsrelations')->all();

The link function itself in the posts table:
public function getLabelsrelations(){
  return $this->hasMany(PostsLabelsRelationships::className(), ['post_id' => 'id']);
}

The labelsrelations link is here and gets the label ids for each post. It remains to reach out to the names.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mikhail Vasiltsev, 2018-12-05
@mihail430899

About via I will read. I did it differently. Added to request:
And in the intermediate PostsLabelsRelationships model, I added a connection with labels.

L
Lander, 2018-12-05
@usdglander

Use viaTable .

D
Dmitry Bay, 2018-12-05
@kawabanga

And I'll add Lander's answer. Yes, viaTable collects properly.
But for the listView component, it's more convenient for me to collect manually.
1) For example, we have the id of all posts.
2) We take away the intermediate table eagerly, or with the help of a query.
3) Requests the ID of the required tags, and keep them in a separate array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question