GridView with relation Data

Hello everyone

I’ve a user table that have a relation with a Role table (1:1) through a relation table called ‘user_role’.

in code terms




public function getRole(){

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

            ->viaTable('user_role', ['id' => 'idOrder']);

    }



i want to show the name of the user role in a GridView




<?=  GridView::widget([

        'dataProvider' => $dataProvider,

        'columns' => [  

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

            'Name',

            'Surname',

            'Email',

            'Role Name Here!',        

            ]

    ]);

?>



how i can set a colum that can retrive the name of the Role?




<?=  GridView::widget([

        'dataProvider' => $dataProvider,

        'columns' => [  

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

            'Name',

            'Surname',

            'Email',

            'role.name',    // role <-- relation name  | name <-- name of field from Role_table     

            ]

    ]);

?>



or you can write simple getRoleName function in your countroller, and do a callback to it throught:




value => function(){ call your method here}