2017-01-21 35 views
0

我有一個模型證書,其中有一個名爲Application的另一個模型的外鍵application_id。所以每個證書都屬於單個應用程序。yii2 Gridview不顯示具有連接屬性的列

現在有一種情況,我想顯示現有用戶的所有證書。用戶標識存在於應用程序模型中,如user_id。

這是現在根據記錄,從上面的查詢未來我想出示證件的一些列和一些使用網格視圖的應用程序的查詢

SELECT * FROM `certificates` 
inner join applications b ON  
application_id = b.id where b.user_id = 7 

。但由於某些原因,如果記錄不止一個,那麼我不會從應用程序獲取任何列數據。

<?php Pjax::begin(); ?> <?= GridView::widget([ 
        'dataProvider' => $dataProvider, 
       //  'filterModel' => $searchModel, 

        'columns' => [ 
         ['class' => 'yii\grid\SerialColumn'], 

         'application_id', 
         'verified_application_file_path', 
         'certificate_name', 
         'application.name', 
         'application.user_id', 


         [ 
          'attribute' => 'creation_date', 
          'format' => ['date', 'php:d/m/Y'] 
         ], 
         [ 
          'attribute' => 'expiry_date', 
          'format' => ['date', 'php:d/m/Y'] 
         ], 

        ], 
       ]); ?> 
       <?php Pjax::end(); ?></div> 

上述網格顯示名稱和用戶ID,如果單個記錄得到返回,否則顯示「未設置」。我不知道爲什麼'application.name'和'application.user_id'在多個記錄收到時不工作。

下面是使用yii2

/** 
* Creates data provider instance with search query applied 
* 
* @param array $params 
* 
* @return ActiveDataProvider 
*/ 
public function search_vendor_certificates($user_id) 
{ 
    $query = ApplicationCertificates::find()->joinWith('application b',true , 'INNER JOIN')->where(["b.user_id"=>$user_id]); 

    // add conditions that should always apply here 


    $dataProvider = new \yii\data\ActiveDataProvider([ 
     'query' => $query, 
    ]); 

return $dataProvider; } 

,我將不勝感激,如果有人能告訴我什麼是我在顯示應用模型的正確屬性做了錯誤我的查詢。所有的

回答

0

優先(不使用這個,我會告訴U上的一個邏輯錯誤):

->joinWith('application b',true , 'INNER JOIN') 

U盤別名applicationb,然後在GridView使用application。無論如何,如果你把它重命名爲bGridView,那還是不錯的。


基於this答案:

型號TableTwo:

public function getTableOneRecord() 
{ 
    return $this->hasOne(TableOne::className(), ['id' => 't1_id']); 
} 

public function getTableThreeRecord() 
{ 
    return $this->hasOne(TableThree::className(), ['id' => 't3_id']); 
} 

public function getTableFourRecord() 
{ 
    return $this->hasOne(TableFour::className(), ['id' => 't4_id']) 
     ->via('tableThreeRecord'); 
} 

和視圖文件:

echo GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'columns' => [ 
     'id', 
     't1_id', 
     'tableOneRecord.id', 
     't3_id', 
     'tableThreeRecord.id', 
     'tableThreeRecord.t4_id', 
     'tableFourRecord.id', 
    ], 
]); 

在說,從搜索你的關係,贏得了」最簡單的方法在GridView中工作,你必須在Model中定義關係,然後使用它們;)

See the code on GitHub