Show data from HasMany-Relation in Gridview

Hello Folks,

i`ve got some questions concerning relations in mysql via yii.

I`m using: yii2

Database: mysql


Ive got three tables. The fist one is (for me) the main. Lets call it "Products". The second one called "Tags". The third one calles "Descripton".

The relations are: One Product may have many Tags. One Product may have one Description.

 -----------------------------      -------------------------     -------------------

|          Products           |    |        Tags             |   |    Desciption     |

|-----------------------------|    |-------------------------|   |-------------------

|id_products (pk)             |    |id_tag (pk)              |   |id_description (pk)|

|Desciption_desciption_id (fk)|    |tag                      |   |description        | 

|name                         |    |Products_id_products (fk)|    -------------------

|bla                          |     -------------------------

|bla2                         |


As you see, in Products i´ve got a direct realtion to Description. So i can call for every product via the description id on Products, the correct Description.

The tags are not so easy. There is no direct relation between Products and Tags.

Question 1:

I managed to build for every table a model. I also managed to build for every table all CRUD files.

If I now take a look at the Products View (GridView) where it should display all the Data from Products and all the realted Data, I can’t find any Tags, weather a tag_id nor the tag itself.

In the Products Model i could find this:

    public function getTags()


        return $this->hasMany(Tags::className(), ['Products_id_products' => 'id_products']);


So there is at least any call to this relation. But somehow in my view (index) i cant find any record for this.

In my opinion this is anywayy not trivial. Since when calling all tags where Products.id_product equals Tags.Products_id_Products, then thery should be given back an array. So i`m not shure if its somehow possible to show this array in one cell of a grid view like this:

id_product|name |bla |bla2 |tag |description|

1 |test |12 |23 |one | dfsdfsdf |

      |     |    |     |two  | asdasdasd |

  1. How can i achieve, that if i would be able to use the hasMany-function, that, instead of retrieving all ID`s that are bound to one tag, i retrieve all tags itself?

I would appreciate any help on this. I searched google now for many hours. Most similar problems where described with yii1 but i think this differs quite a lot form yii2 since i couldnt get those solutions to work.


Gii’s CRUD generator won’t include the related models automatically. You have to add some code to show them.

Please read the following section of the guide. >

Don’t waste your time with googling.

You must use a function inside the GridView column to show all tags, using the necessary relation(s) to get to the attribute you want to display.

Something like this (not tested, may require some tweaks)




    'label' => 'Tags',

    'value' => function($model){

        return join(', ', yii\helpers\ArrayHelper::map($model->tags, 'id_tag', 'tag'));





Take a look to this:

My link

Thanks for the fast replies.

I will check if I can realize it with your replies.