How to use Yii2 kartik gridview editable column with mulitple model relations

9.6k views Asked by At

Question:

I have 2 models, user model and user profile model, both are related with primary and forien key user_id, user table has fields id,username, password, email etc user_profile table has filed id, user_id, company, phone etc

I have created gridview in user view. I am using kartik EditableColumn, I want to edit user profile table field company from user gridview.

What I have done so far. Please check code what I have done.

please note: it is portion of code i am showing here, and I think it is enough to understand. if you like to see more i will edit question.

use yii\helpers\Url;
$gridColumns = [
     [
    'class' => 'kartik\grid\EditableColumn',
    'attribute' => 'company',
    'pageSummary' => true,
    'readonly' => false,
    'value' => 'userProfiles.company',
    'content' => function($data,$model){return '<div class="text_content">'.htmlentities($data->userProfiles->company).'</div>';},
    'editableOptions' => [
            'header' => 'Company',
            'inputType' => kartik\editable\Editable::INPUT_TEXT,
            'options' => [
                'pluginOptions' => [

                ]
            ]
        ],
    ],  

    ];

//note: used all namespace

 Pjax::begin(); 
 GridView::widget([

    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'class' => 'kartik\grid\EditableColumn',
    'responsive'=>true,
    'hover'=>true,
    'pjax'=>true,
    'tableOptions'=>['class'=>'table table-bordered table-striped dataTable table-hover'],
    'summaryOptions' => ['class' =>'dataTables_info'],
    'layout'=>"{items}\n<div class='row' style='margin:0.1%'>
                         <div class='col-sm-5'>
                            {summary}
                         </div>
                         <div class='col-sm-7'>
                            <div class='dataTables_paginate paging_simple_numbers'>{pager}</div>
                         </div>
                         </div>",
    'columns' => $gridColumns,
]); Pjax::end(); 

Problem is

When I click on company value it shows me blank. please check image.enter image description here

2

There are 2 answers

0
bhaveshv05 On BEST ANSWER

In User Model, Is there model relation you have mentioned? no, define relation between them as followed.

public function getUserProfiles()
{
    return $this->hasOne(UserProfile::className(), ['user_id' => 'id']);
} 

Now create an assigning value method in same model as below

public function getProfileCompany(){
    return $this->company = $this->userProfiles->company;
} 

Now in Gridview use getProfileCompany method with assigning value as below

   use yii\helpers\Url;
   $gridColumns = [
   [
   'class' => 'kartik\grid\EditableColumn',
   'attribute' => 'company',
   'pageSummary' => true,
   'readonly' => false,
   'value' => function($model){ return $model->profileCompany; }, // assign value from getProfileCompany method
   'editableOptions' => [
        'header' => 'Company',
        'inputType' => kartik\editable\Editable::INPUT_TEXT,
        'options' => [
            'pluginOptions' => [

            ]
        ]
    ],
],  

];
2
lndim On

Try to add the method described below to your User model:

public function getCompany()
{
    return $this->userProfiles->company;
}