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;
}
解决方案
推荐阅读
- scala - 由于重复列,Spark 读取失败
- javascript - Formik 处理对象数组
- java - 控制台不显示“原因:”注释
- python - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (512,) 和 (256,)
- java - 我想在java中使用递归打印一个系列
- julia - BoundsError:尝试访问索引 [2n+1] 处的 2n 元素 UnitRange{Int32}
- python - BLPAPI 和彭博掉期经理
- php - 从数据库 Laravel Notifications 在视图刀片上显示数据
- sql - 查询在 sql developer 中工作,但在 sql plus 中不工作,它应该抛出错误
- c++ - C++ 中的当前时间和日期 20 天