Model relation data in gridview?

HI guys,

This is probably something really ease but I can not get it to work.

I have a model (A) with a relation to an other model (B). I want to fill my grid with the data from A and from B. But I can not access the data from B. For some reason when I try to access data from model B, it says I am trying to get a property of a non object. Strange is is though. If I print A->B i see B as my object including with the data.

How do I show data from model B in my grid view? Using $a->getBcontent->id is not working.

Post relation between A and B and gridview’s column where you try to display B’s data.

This is the relation


public function getClassContents()


        return $this->hasOne(classContent::className(), ['id' => 'id']);


This is the part in the grid:

['attribute' => 'name',

                    'label' => Yii::t('app','Name'),

                    'format' => 'raw',

                    'value' => function ($data) {

                        return $data->classContents->name;



Are you sure that keys are ‘id’ for both models?

Yes, I use the same id and value for the relation. I can see the data of the relation but I can not access it. :-s

Are you sure that are related records?

Try to enable log to view raw sql.

Hmmm, I got it fixed. I deleted the tables and re-created them in the database and defined the relations again and now it works. I think I screwed around with the table structure to much before. It must have messed something up.

Thanks for the help!