0

我正在yii2我的项目中工作,我有用户和他们的角色。每个角色都可以访问一个模块和一个子菜单。有一个名为的子菜单,可以SIM List在其中查看所有SIM记录。有一个名为的字段issued_to告诉我们哪个 SIM 已发给哪个用户。

除非向任何用户发放 SIM 卡,否则该issued_to字段将保持为空。一旦发布,用户的名字就会出现在SIM List.

现在我想以只有特定用户才能看到列表的方式对其进行管理。例如5 Sims已发给名为U. 现在用户U应该只看到发给他的 SIM 记录,否则列表应该是空的。

在我的Index控制器中,我得到issued_to的字段名称默认为空。

 public function actionIndex()
{
    $searchModel = new SimsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

通过执行以下操作,我可以获得issued_to用户 ID

$model = $dataProvider->getModels()[0];
$user_id =  $model['issued_to'];
var_dump($user_id);
exit();

现在在这个控制器中,我想添加一个检查,user_id它只给我那个特定用户的记录。

索引视图

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

        //'id',
        'imsi',
        'sim_number',
        'operator_name',
        'data_details',
        'sms_details',
        'monthly_bill',
        //'created_by',
        [
            'label' => 'Created By',
            'value' => function ($data) {
                if (is_object($data))
                    return $data->created->name;
                return ' - ';
            },
            //'filter' => Html::activeDropDownList($searchModel, 'created_by', \app\models\User::toArrayList(), ['prompt' => "Created By", 'class' => 'form-control']),
        ],
        'created_at',
        // 'updated_at',
        'status',
        // 'updated_by',
        //'sim_stauts',
        [
            'label'=>'SIM Status',
            'value'=>function($a){
                return $a->getStatusvalue();
            }
        ],
        //'issued_to',
        [
            'label' => 'Issued To',
            'value' => function ($d) {
                if(is_object($d->user))
                    //return $d->user->name;
                    return $d->issued_to == '' ? '' : $d->user->username;
                return ' - ';
                // return $d->user->name;

            },
            'filter' => Html::activeDropDownList($searchModel, 'issued_to', \app\models\User::toArrayList(), ['prompt' => "Users", 'class' => 'form-control']),

        ],
       //'returned_by',
        [
            'label' => 'Returned By',
            'value' => function ($d) {
                if(is_object($d->user2))
                    //return $d->user->name;
                    return $d->returned_by == '' ? '' : $d->user->username;
                return ' - ';
                // return $d->user->name;

            },
            'filter' => Html::activeDropDownList($searchModel, 'returned_by', \app\models\User::toArrayList(), ['prompt' => "Users", 'class' => 'form-control']),

        ],
        'historic',

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

更新 1

search model的在下面

public function search($params)
{
    $query = Sims::find();

    // add conditions that should always apply here

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

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'id' => $this->id,
        'created_by' => $this->created_by,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
        'updated_by' => $this->updated_by,
        'sim_stauts' => $this->sim_stauts,
        'issued_to' => $this->issued_to,
        'returned_by' => $this->returned_by,
        'historic' => $this->historic,
    ]);

    $query->andFilterWhere(['like', 'imsi', $this->imsi])
        ->andFilterWhere(['like', 'sim_number', $this->sim_number])
        ->andFilterWhere(['like', 'operator_name', $this->operator_name])
        ->andFilterWhere(['like', 'data_details', $this->data_details])
        ->andFilterWhere(['like', 'sms_details', $this->sms_details])
        ->andFilterWhere(['like', 'monthly_bill', $this->monthly_bill])
        ->andFilterWhere(['like', 'status', $this->status]);

    return $dataProvider;
}

我怎样才能实现它?任何帮助将不胜感激。

4

1 回答 1

1

根据您的描述,您已user_id保存在该issued_to字段中,您只需要获取当前登录user_id保存在该字段中的结果issued_to

我假设您在网格视图中的搜索仅对登录用户可见。

您应该通过首先从具有与具有相同格式的数组的对象获取数组来手动设置参数,因此您issued_to需要将其设置为queryParamsrequestYii::$app->request->queryParams;POST['ModelName']['field_name']issued_to

$arrayParams['SimSearch']['issued_to']=Yii:$app->user->id;

actionIndex应该如下所示

public function actionIndex()
{
    $searchModel = new SimsSearch();
    $queryParams=Yii::$app->request->queryParams;

    //check if user or one of the managers
    $isAdmin=in_array(Yii::$app->user->identity->user_role,[1,6]);

    //set params if normal user
    if(!$isAdmin){
       $queryParams['SimsSearch']['issued_to']=Yii::$app->user->id;
    }

    $dataProvider = $searchModel->search($queryParams);

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

希望这可以帮助你。

于 2018-02-28T21:07:36.070 回答