2016-11-05 37 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