Gridview With Table Relationship

Hello,

I was trying to show a gridview with a relationship column. The CRUD generator add the "employee_type_id" column but I need to show the type name instead of type ID.




<?php echo GridView::widget([

		'dataProvider' => $dataProvider,

		'filterModel' => $searchModel,

		'columns' => [

			['class' => 'yii\grid\SerialColumn'],

			'name',

			'last_name',

                        'employee_type_id',


			['class' => 'yii\grid\ActionColumn'],

		],

	]); ?>

I change the $query of the Search method


 $query = Employee::find()->leftJoin('employee_type','employee.employee_type_id=employee_type.id')->with('EmployeeType');

And I change the sort like the topic http://www.yiiframework.com/forum/index.php/topic/49447-how-to-use-filters-in-the-gridview-widget-when-table-relationship-is-present/


$dataProvider = new ActiveDataProvider([

			'query' => $query,

            'sort' => array(

                'attributes' => array(

                    'EmployeeType' => array(

                        'asc' => array('EmployeeType.name' => SORT_ASC, 'name' => SORT_ASC),

                        'desc'=> array('EmployeeType.name' => SORT_DESC, 'name' => SORT_ASC)

                    ),

                    'name',

                    'last_name',

                )

            ),

		]);

What should I write in the GridView column?? Because ‘EmployeeType’ does not work.




<?php echo GridView::widget([

		'dataProvider' => $dataProvider,

		'filterModel' => $searchModel,

		'columns' => [

			['class' => 'yii\grid\SerialColumn'],

			'name',

			'last_name',

                        'EmployeeType', 


			['class' => 'yii\grid\ActionColumn'],

		],

	]); ?>

Thanks a lot for your help!

The best way I’ve found is to use the closure definition.




[

    'attribute' => 'employee_type_id',

    'label' => 'Employee Type',

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

        return $model->EmployeeType->name;

    },

],



Great!!! Thanks a lot amnah. I could not find the solution anywhere.

Your example works great with GridViews and with DetailView I use:


<?php echo DetailView::widget([

		'model' => $model,

		'attributes' => [

			'id',

			'name',

			'last_name',

            [

                'attribute' => 'employee_type_id',

                'label' => 'Employee Type',

                'value' =>  $model->EmployeeType->name

            ],

		],

	]); ?>

If somebody has a better solution please let us know.

I had posted a similar reply with couple of methods by which you can do this currently… but there are other ways as well. Refer this post of mine.

Did it work the Method 1?

I Set up the Employee model relation:


/**

 * @return \yii\db\ActiveRelation

 */

public function getEmployeeType()

{

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

}

I Define a getter method in my Employee model:


public function getEmployeeTypeName() {

        return $this->employeeType->name;

}

And I Call this in my gridview:


<?php echo GridView::widget([

		'dataProvider' => $dataProvider,

		'filterModel' => $searchModel,

		'columns' => [

			['class' => 'yii\grid\SerialColumn'],

			'name',

			'last_name',

                        [

                             'attribute' => 'employee_type_id',

                             'label'=>'Employee Type',

                             'value'=>'employeeTypeName'

                        ],


			['class' => 'yii\grid\ActionColumn'],

		],

	]); ?>

But It does not work, I get the following error:


Warning – yii\base\ErrorException


call_user_func() expects parameter 1 to be a valid callback, function 'employeeTypeName' not found or invalid function name

You need to change it to:




[

	 'attribute' => 'employeeTypeName',

	 'label'=>'Employee Type',

],



Note: In this case, the column filtering will not work (since you are using a getter method to fake an attribute).

Thanks Kartik V!!! Great Example. I think this is the best solution.

Displaying, Sorting and Filtering Model Relations on a GridView