YII2 gridview不显示正确的数据

时间:2017-01-06 12:20:21

标签: php gridview yii2

我有几张桌子。职位(持有所有职位),申请人(持有所有申请人)和assign_applicant(说明哪个职位由申请人持有 - 多人重申)。

因此数据类似于assign_applicant表

pos  applicant
2      1
2      3
3      4
3      5

我想要显示这样的记录。但相反它显示:

pos  applicant
2    1,3 (array)
3    4,5 (array)

原因是(很多)关系:

 public function getAssign()
    {
        return $this->hasMany(\admin\models\AssignApplicant::className(), ["job_position_id" => "id"]);
    }

    public function getApplicant()
    {
        return $this->hasOne(\admin\models\Applicant::className(), ["id" => "applicant_id"]) ->via('assign');
    }

这是网格视图:

 <?=
    GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'layout' => "{pager}\n{summary}\n{items}\n{pager}",
        'filterPosition' => \yii\grid\GridView::FILTER_POS_HEADER,
        'responsive' => true,
        'hover' => true,
        'resizableColumns' => true,
        'floatHeader' => true,
        'autoXlFormat' => true,
        'export' => [
            'fontAwesome' => true,
            'showConfirmAlert' => true,
            'target' => GridView::TARGET_BLANK
        ],
        'panel' => [
            'type' => 'primary',
            'heading' => ''
        ],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute' => 'position',
                'format' => 'raw',
                'value' => function ($model)
                {
                    return Html::a($model->positionName, yii\helpers\Url::toRoute(['position/update', 'id' => $model->position_id]));
                }
                    ],
                    [
                        'attribute' => 'applicantName',
                        'value' => 'applicantName'
                    ],

                ],
            ]);
            ?>

我的查询(原始表格):

SELECT  `job_positions` . * , applicant.first_name, assign_applicant.id
FROM  `job_positions` 
LEFT JOIN  `position` ON  `job_positions`.`position_id` =  `position`.`id` 
INNER JOIN  `assign_applicant` ON  `job_positions`.`id` =  `assign_applicant`.`job_position_id` 
INNER JOIN  `applicant` ON  `assign_applicant`.`applicant_id` =  `applicant`.`id` 
WHERE (
`client_id` =  '1'
)
AND (
`shift_id` =  '1'
)
AND (
`date` =  '2016-12-08'
)
ORDER BY  `position`.`name` 
LIMIT 20

以任何方式显示我想要的数据

1 个答案:

答案 0 :(得分:0)

yii2在处理结果时故意removes duplicated models

您可以使用applicants(或assign_applicant)作为@Bizley建议的主要模型 或事先处理结果模型(使用克隆和populateRelation来获得所需的结构)