如何在GridView Yii2中获取合并的数组列表数据

时间:2015-04-08 12:55:58

标签: php yii2

这里我有两张桌子

  1. Employeedetails
  2. Claimprocess
  3. 在employeedetails中,我有8列:

    Company_name,Employeecode,Employee_name,father_name,mother_name,wife_name,child1_name和child2_name

    在Claimprocess表中我有5列:

    Company_name,Employeecode,Employee_name,Healthcard_no和claim_for

    在Claimprocess中,我为employeecode设置了一个依赖下拉列表。例如,如果admin选择自动显示依赖employeecode的company_name,则admin再次选择一个自动显示employee_name和employee关系名称的employeecode,例如father_name,mother_name,wife_name等...

    这是employee_code的依赖下拉代码,方法是选择company_name

    claimform.php

    <?= $form->field($model, 'company_id')->widget(Select2::classname(), [
                'data' => ArrayHelper::map(Company::find()->all(),'id','companyname'),
                'language' => 'en',
                'options' => [
                'placeholder' => 'Select a company ...',
                'onchange' => '
                                    $.post( "index.php?r=employeedetails/lists&id='.'"+$(this).val(), function( data ) {
                                      $( "select#claimprocess-employee_id" ).html( data );
                                    });',
                ],
                'pluginOptions' => [
                    'allowClear' => true
                ],
            ]); 
    ?>
    

    employeedetailscontroller.php

    public function actionLists($id)
    {
        $countEmployeedetails = Employeedetails::find()
                ->where(['company_id' => $id])
                ->count();
    
        $employeedetails = Employeedetails::find()
                ->where(['company_id' => $id])
                ->all();
    
        if($countEmployeedetails>0){
            foreach($employeedetails as $employee){
                echo "<option value='".$employee->id."'>".$employee->employeecode."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }
    
    }
    

    这是employee_name和relations_name的相关下拉代码,方法是选择employeecode

    claimform.php

    <?= $form->field($model, 'employee_id')->widget(Select2::classname(), [
                'data' => ArrayHelper::map(Employeedetails::find()-   >all(),'id','employeecode'),
                'language' => 'en',
                'options' => [
                'placeholder' => 'Select a employeecode ...',
                'onchange' => '
                                    $.post( "index.php?r=employeedetails/lists2&id='.'"+$(this).val(), function( data ) {
                                      $( "select#claimprocess-claim_for" ).html( data );
                                    }),
                                    $.post( "index.php?r=employeedetails/lists1&id='.'"+$(this).val(), function( data ) {
                                      $( "select#claimprocess-employee_name" ).html( data );
                                    });',
                ],
                'pluginOptions' => [
                    'allowClear' => true
                ],
            ]); 
    ?>
    

    employeedetailscontroller.php

    public function actionLists1($id)
    {
        $countEmployeedetails = Employeedetails::find()
                ->where(['id' => $id])
                ->count();
    
    
        $employeedetails = Employeedetails::find()
                ->where(['id' => $id])
                ->all();
    
        if($countEmployeedetails >= 0)
        {
            foreach($employeedetails as $employee)
            {
                echo "<option value='".$employee->id."'>".$employee->name."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }
    
    }
    
    public function actionLists2($id)
    {
    
        $countEmployeedetails = Employeedetails::find()
                ->where(['id' => $id])
                ->count();
    
    
        $employeedetails = Employeedetails::find()
                ->where(['id' => $id])
                ->all();
    
        if($countEmployeedetails >= 0)
        {
            foreach($employeedetails as $employee)
            {
                    echo "<option value='".$employee->id."'>".$employee->father_name."</option>";
                    echo "<option value='".$employee->id."'>".$employee->mother_name."</option>";
                    echo "<option value='".$employee->id."'>".$employee->wife_name."</option>";
                    echo "<option value='".$employee->id."'>".$employee->child1_name."</option>";
                    echo "<option value='".$employee->id."'>".$employee->child2_name."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }
    }
    

    在claim_for字段中,我必须选择特定员工的关系名称,所以我所做的是使用array_merge组合所有关系字段,例如father_name,mother_name等,并且我在表单中得到了一个名称。

    claimform.php

    <?php
              $em = ArrayHelper::map(Employeedetails::find()-  >all(),'id','father_name');
             $emp = ArrayHelper::map(Employeedetails::find()->all(),'id','mother_name');
            $emp1 = ArrayHelper::map(Employeedetails::find()->all(),'id','wife_name');
            $emp2 = ArrayHelper::map(Employeedetails::find()->all(),'id','child1_name');
            $emp3 = ArrayHelper::map(Employeedetails::find()->all(),'id','child2_name');
    
            $print = array_merge($em,$emp,$emp1,$emp2,$emp3);
            // echo "<pre>";print_r($print);exit();echo "</pre>";
    
            echo $form->field($model, 'claim_for')->dropDownList($print,['option' => '']); 
    ?>
    

    问题:

    在选择关系时,我在表单中获取所有关系名称,但在gridview中保存时,我得到employee_id而不是merge_array id。

    所以我在这里使用这段代码获取关系名称,但我不知道我的编写代码是对还是错:

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
    
            //'id',
            'company.companyname',
            'employee.employeecode',
            'employee.name',
            'claim_for',
            [
               'attribute' => 'claim_for',
               'format' => 'raw',
               'value' => function($model, $key, $index, $grid) {
                    $temp = $model->claim_for;
                    $si = Employeedetails::find()->where(['id' => $temp])->one();
                    return $si['father_name'];// here how to bring merged array relation name
    
            },  
            ],
            'healthcard_no',
    
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    

    我一直在寻找两天,但我无法得到它。如果有人可以提供帮助,我会很感激。

0 个答案:

没有答案