ActionColumn with dataprovider for join tables

Dear all

I have a searchmodel and a dataprovider for two tables which I join using a junction tabel. I can get the data in the GridView, but the problem is that the ationcolumn which is generated, takes a newly generated id to show (view the model) and not that of the original model.

example

The join statement




        $query = (new Query())->select(['contactpersoon.id as id','contactpersoon.naam AS Contactnaam', 'contactpersoon.voorNaam AS Contactvoornaam', 'klant.naam AS Klantnaam'])->from('klant')->

                innerJoin('klant_heeft_contactpersoon', 'klant.id = klant_heeft_contactpersoon.klantId')->

                innerJoin('contactpersoon', 'contactpersoon.id = klant_heeft_contactpersoon.contactpersoonId');



The GridView




$gridColumns = ['id',

    'Klantnaam',

    'Contactvoornaam' => 'Contactvoornaam',

    'Contactnaam',

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

        'controller' => 'contactpersoon'],

];


echo GridView::widget([

    'dataProvider' => $dataProvider,

    'filterModel' => $searchModel,

    'columns' => $gridColumns,

]);



That gives me a row of the joined table. Now, when pressing view I want to see the model of the


contactpersoon

but the id it uses to show the contactpersoon is not correct.

How can I solve this?

Kind regards

Jens Buysse

Set a model relationship and then you can access the data like this


            [

                'class' => 'yii\grid\DataColumn',

	            'label'=>'contact person name',

	            'format'=>'raw',

	            'value'=>$function($data){

		            return $data->contactpersoon->name

	            }

        

            ],

Thanks! I will check that out tonight.

Kind regards

Jens Buysse