2016-11-05 38 views
1

如何顯示基於當前用戶登錄索引的gridview?例如,學生A登錄並填寫表單並註銷。當其他學生登錄時,學生A填寫的數據將不會顯示。顯示基於yii2用戶登錄的gridview

在GridView控件

 <?= GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'columns' => [ 
     ['class' => 'yii\grid\SerialColumn'], 

     'education_id', 
     'year', 
     'fieldstudy', 
     'institute', 
     'grade', 

     ['class' => 'yii\grid\ActionColumn'], 
    ], 
]); ?>  

在控制器

 public function actionIndex() 
{ 
    $dataProvider = new ActiveDataProvider([ 
     'query' => Education::find()-> 
       where(['user_id' => Yii::$app->user->identity->id ]), 
    ]); 
    ]); 

    return $this->render('index', [ 
     'dataProvider' => $dataProvider, 
    ]); 
} 
} 

在模型

class Education extends ActiveRecord implements IdentityInterface 
    { 

public static function tableName() 
{ 
    return 'education'; 
} 


public function rules() 
{ 
    return [ 
     [['year', 'fieldstudy', 'institute', 'grade'], 'required'], 
     [['user_id'], 'integer'], 
     [['year', 'fieldstudy', 'institute', 'grade'], 'string', 'max' => 255], 
    ]; 
} 


public function attributeLabels() 
{ 
    return [ 
     'education_id' => 'Education ID', 
     'student_id' => 'Student ID', 
     'year' => 'Year', 
     'fieldstudy' => 'Fieldstudy', 
     'institute' => 'Institute', 
     'grade' => 'Grade', 
    ]; 
} 

public function getId($id) 
{ 
    return static::find(['user_id' => $id]); 

} 

回答

0

如果需要retrive相關的用戶ID和假定教育信息的教育模式此關係基於您可以使用的字段user_id Yii::$app->user->identity->id

$dataProvider = new ActiveDataProvider([ 
    'query' => Education::find()-> 
        where(['user_id' => Yii::$app->user->identity->id ]), 
]); 
+0

..它仍然失敗。我有這個錯誤。 「query」屬性必須是實現QueryInterface的類的實例,例如yii \ db \ Query或其子類。在教育模式,我需要把user_id?我仍然混淆。 @scaisEdge – Fyp16

+0

我已更新答案..最終更新您的帖子並添加教育模型代碼以及與您相關的控制器/操作代碼qquestion – scaisEdge

+0

我已編輯我的帖子並附加了教育模型部分。我如何修復模型部分? @scaisEdge – Fyp16