如何在 yii2 中根据登录用户更新、查看、删除?

How to Update,View,Delete as per login user in yii2?

我必须根据登录用户更改查看、更新、删除的 ID,但是当我单击“查看”时,我得到 index.I 的 ID 想根据 ID 更改特定公司..请帮助我我是 yii2 的新手...提前致谢。 actionIndex()

$searchModel = new VendorsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->pagination->pageSize = 10;
        $user = \Yii::$app->user->identity;
        $userid = \Yii::$app->user->identity->id;

         $query1 = new \yii\db\Query;
            $query1->select('*')->from('vendors')->where(['ven_contact_person_id' => $userid,'deleted' => 'N']);
            $query1->createCommand();

            $dataProvider1 = new ActiveDataProvider([
                'query' => $query1,
                'pagination' => false,
            ]);



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

index.php

    <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'dataProvider' => $dataProvider1,
            'filterModel' => $searchModel,
            'columns' => [
            'ven_id',
            'ven_company_name',
            'ven_website',
             'ven_contact_no',
             'ven_email_id:email',
          ['class' => 'yii\grid\ActionColumn',
              'header' => 'Action',
              'template' => '{view} {edit} {delete}',
              'buttons' => [
                 'view' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url);
                 },
                 'edit' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url);
                 },
                 'delete' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url);
                 },
              ],
              'urlCreator' => function ($action, $dataProvider, $key, $index) {

                if ($action === 'view') {
                     return Url::to(['vendors/view', 'id' =>$dataProvider['ven_id']]);
                }
                if ($action === 'edit') {
                    return Url::to(['/vendors/update', 'id' =>$dataProvider['ven_id']]);
                }
                if ($action === 'delete') {
                   return Url::to(['/vendors/delete', 'id' =>$dataProvider['ven_id']]);
                }                    
                return $url;
            }
          ],
      ],
]); ?>

抱歉添加 "answer"(无法添加评论,它需要信誉 >= 50)

你能添加索引视图渲染文件吗?

在我看来,这将是该文件中的一个错误,例如对我有用的方法:

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        'name',
        ['class' => 'yii\grid\ActionColumn',
            'header' => 'Action',
            'template' => '{view} {edit} {delete}',
            'buttons' => [
               'view' => function ($url, $model) {
                 return Html::a('View', $url);
               },
               'edit' => function ($url, $model) {
                 return Html::a('Edit', $url);
               },
               'delete' => function ($url, $model) {
                 return Html::a('Delete', $url);
               },
            ],
        ],
    ],
]);

对于图标和模型->id,我想你需要这个

   GridView::widget([
      'dataProvider' => $dataProvider,
      'filterModel' => $searchModel,
      'columns' => [
          'name',
          ['class' => 'yii\grid\ActionColumn',
              'header' => 'Action',
              'template' => '{view} {edit} {delete}',
              'buttons' => [
                 'view' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url);
                 },
                 'edit' => function ($url, $model) {
                   return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url);
                 },
                 'delete' => function ($url, $model) {
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url,[
                    'title' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                ]);
                 },
              ],
              'urlCreator' => function ($action, $model, $key, $index) {

                if ($action === 'view') {
                    $url = Url::to(['/yourController/view', 'id' =>$model->id]);
                }
                if ($action === 'edit') {
                    $url = Url::to(['/yourController/edit', 'id' =>$model->id]);
                }
                if ($action === 'delete') {
                    $url = Url::to(['/yourController/delete', 'id' =>$model->id]);
                }                    
                return $url;
            }
          ],
      ],
  ]);