Yii2索引查看自己的数据

时间:2019-04-10 08:43:40

标签: php 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;
    }

0 个答案:

没有答案