[SOLVED] GridView - display value in column from relation

Hi

I’ve been playing with Yii-1 back in the days. Then I had a break for long time. And now trying to write simple CRUD app with Yii-2.

My problem is to display value from relation in view. I’ve searched forums and haven’t found solution.

I have a model and relation defined in it to access data from another table.

In Yii-1 I was accessing the relation data




		array(              

	        'name'=>'id_manager_type',

		'header'=>'Manager Type',

        	'value'=>'$data->rel_id_manager_type->name_en',

		),



Where rel_id_manager_type was relation defined in master model. And name_en was column name in related table.

In Yii-2 I am trying to access relation data:




            [

             'attribute'=>'Business Type',

             'format'=>'raw',

             'value' => Html::encode($data->businessTypes->name_en),

	    ],



But it returns


Undefined variable: data

What am I doing wrong?

OK got it. Simply:


'businessTypes.name_en',

Where businessTypes is relation to another table and name_en is column name with desired value.

Or, if you want to be more complex, you can use like this:




'value' => function ($model, $key, $index, $column) {

    return ($model->supAmount == 0 ? null : $model->transaction->transactionsTotal);

},