N
N
nepster-web2014-04-23 18:56:52
Yii
nepster-web, 2014-04-23 18:56:52

Yii2 - how to make a double relation?

The task is this, there are 3 tables: articles, tags and articles_tags (link table) articles_tags contains
3 columns:
- id
- article_id
-
tag_id
id=1) and it has 2 tags (tree (tag_id=1) and hollow (tag_id=2)), in the link table we will have 2 entries:
1 - 1 (tree tag for article 1)
1 - 2 (hollow tag for an article 1)
I need to get all the tags for my article, I wrote this getter:

public function getTags()
    {
        return $this->hasMany(ArticlesTags::className(), ['article_id' => 'article_id']);
    }

But the campaign is only for the link table, but how else to get the tags themselves?
On pure sql, it's easier, 2 joins, but how to do it using Yii2?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Morozov, 2014-04-23
@nepster-web

In general, this is described in detail in the documentation.
See viaTable and via:
https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question