I'm going to use 1st normalization form in my Yii2 project, so I've added table like this
| id | post_id | tag_id |
and when I'm to posts model I've writed this:
public function getTags()
{
return $this->hasMany(PostTags::className(), ['post_id' => 'id']);
}
In view widget I've added 'tags.tag_id', but it shows no data.
Is there any way to show this tags in DetailView and GridView widgets?
May be, I can write "group_concat" somewhere?
I'd recommend to write a widget for displaying a links list of related records. It's reusable, prevents HTML generation in model / controller, reduces the amount of code in view.
Here are some examples (assuming tag name is stored in
name
column).Usage in
GridView
:Usage in
DetailView
:Don't forget to set format
raw
, because by default content is rendered as plain text to prevent XSS attacks (html special characters are escaped).You can modify this to fit your needs, this is just an example.