首页 > 解决方案 > Yii2索引查看自己的数据

问题描述

只要至少有一条记录,它就可以正常工作,但会引发错误,这似乎很明显,但我无法找到修复它的方法。

我使用的代码是这样的:

public function actionIndex()
    {
        $searchModel = new CommandsSearch();
        $cond = "user_id= " . Yii::$app->user->identity->id;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$cond);
        $dataProvider->query->andWhere('user_id =' . Yii::$app->user->identity->id);

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

我想显示带有添加记录选项的 gridview 页面。

更新:

return [
         'access' => [
            'class' => AccessControl::classname(),
            'only'  => ['index','view','create','update','delete'],
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['admin','manager','user'],                        
                    'matchCallback' => function ($rule, $action) {
                            if (Yii::$app->user->can('admin') || $this->isUserAuthor()) {
                                return true;
                            }
                            return false;
                        }
                ],
            ],
            'denyCallback' => function ($rule, $action) {
            throw new \Exception('You are not allowed to access this page');
                    }                
        ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

和 isUserAuthor 方法如:

protected function isUserAuthor()
    {   
       // $author= $this->findModel($_GET["id"]); 
        $author= Commands::findOne(['user_id'=>\Yii::$app->user->identity->id]);
       // var_dump($author->user_id);exit;
        return $author;
    }

标签: phpyii2

解决方案


推荐阅读