我有一个超级管理员,可以创建用户,可以为每个用户分配特定的位置。这被存储在一个简单的表中,我们在其中获得用户的ID和位置的ID。
然后,我有一个客户表。每个客户机还有一个名为loc_id的位置。如何进行过滤,以便在Gridview中,用户只能看到位于该用户有权查看的位置的客户端(用户是一种管理员,可以在其管辖范围内操纵客户端的数据)?
我通过
获取用户id$userId = 'Yii::$app->user->identity->id
,然后找到他可以使用
查看的位置。$userLocation = Userlocations::find()-where(['user_id' => $userId])->all();
但是现在我怎么能把它应用到一个标准的Gridview与搜索模型和数据提供程序,这样我仍然可以过滤和排序Gridview的其他属性?
$searchModel = new AsociatiiSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
首先在associatiisearch类中添加一个公共属性:
class AsociatiiSearch {
public $userLocation;
...
然后在控制器动作中找到userLocation:
$userId = 'Yii::$app->user->identity->id
$userLocation = Userlocations::find()-where(['user_id' => $userId])->all();
$searchModel = new AsociatiiSearch();
$searchModel->userLocation = $userLocation;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
最后,在associatiisearch搜索方法上,您可以使用全局userLocation:
添加另一个过滤器$query->andFilterWhere(['userLocation' => $this->userLocation]);