2017-01-06 184 views
0

我有幾個表。職位(擔任所有職位),申請人(擔任所有申請人)和assign_applicant(說明哪個職位由什麼申請人持有 - 多至多人)。YII2 gridview不顯示正確的數據

所以數據是像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 

任何方式來顯示我想要的數據

+0

添加的代碼你的GridView。你也有一個代表'assign_applicant'表的模型嗎? – topher

+0

@topher - 添加代碼 – Ish

+1

與相關申請人一起顯示職位,而不是顯示相關職位的申請人。 – Bizley

回答

0

yii2故意在處理結果時removes duplicated models

您既可以使用applicants(或assign_applicant)作爲主模型作爲@Bizley建議 或預先處理由此產生的模型(使用克隆和populateRelation以獲得所需的結構)