沃毕尼闷兜率
- 沃毕尼闷兜率 收藏了教程Yii2 生成纯静态html文件
- 沃毕尼闷兜率 赞了评论
简单点说,就是验证顺序的问题:
如果在模型里只写了rules 验证(当然只写rule也是可以自定义场景的),那么就去rules里验证表单数据。
如果在模型里又写了 scenarios 那么验证要先去 scenarios里验证,如果验证到了,就不会去rules里再验证了。例如:
public function rules(){ return [['name','age'],'required','message'=>'name不能为空','on'=>'create'] } public function scenarios(){ return ['create'=>['name']]; }
如果使用模型的地方指定了create场景,那么只会验证name不能为空,提交表单的时候age为空也可以提交。
如果把scenarios 注释掉。那么再提交表单的时候。验证会提示name 和 age 都不能为空。 $filter = new ActiveDataFilter([ 'searchModel' => 'app\models\PostSearch' ]); $filterCondition = null; // 您可以从任何来源加载过滤器。例如: // 如果你更喜欢请求体中的 JSON, // 使用 Yii::$app->request->getBodyParams() 如下: if ($filter->load(\Yii::$app->request->get())) { $filterCondition = $filter->build(); if ($filterCondition === false) { // Serializer would get errors out of it return $filter; } } $query = Post::find(); if ($filterCondition !== null) { $query->andWhere($filterCondition); } return new ActiveDataProvider([ 'query' => $query, ]);
来自 权威指南 https://www.yiiframework.com/doc/guide/2.0/zh-cn/output-data-providers
gii自带的那套就已经有组装where条件的代码了,我随便找了一个表生成的代码案例
public function search($params) { $query = Bargain::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, 'member_id' => $this->member_id, 'sub_order_id' => $this->sub_order_id, 'order_id' => $this->order_id, 'rule_id' => $this->rule_id, 'price' => $this->price, 'balance' => $this->balance, 'status' => $this->status, 'task_status' => $this->task_status, 'create_time' => $this->create_time, 'modify_time' => $this->modify_time, 'limit_time' => $this->limit_time, 'finish_time' => $this->finish_time, 'company_id' => $this->company_id, 'self_balance' => $this->self_balance, 'friends_balance' => $this->friends_balance, ]); $query->andFilterWhere(['like', 'address', $this->address]) ->andFilterWhere(['like', 'bn', $this->bn]) ->andFilterWhere(['like', 'sub_bn', $this->sub_bn]) ->andFilterWhere(['like', 'cancel_reason', $this->cancel_reason]); return $dataProvider; }
https://api.zhangmoxuan.com/yii-data-arraydataprovider.html
$query = new Query; $provider = new ArrayDataProvider([ 'allModels' => $query->from('post')->all(), 'sort' => [ 'attributes' => ['id', 'username', 'email'], ], 'pagination' => [ 'pageSize' => 10, ], ]); // get the posts in the current page $posts = $provider->getModels();
老哥,是ActiveDataFilter的用法,不是array
$filter = new ActiveDataFilter([ 'searchModel' => 'app\models\PostSearch' ]); $filterCondition = null; // 您可以从任何来源加载过滤器。例如: // 如果你更喜欢请求体中的 JSON, // 使用 Yii::$app->request->getBodyParams() 如下: if ($filter->load(\Yii::$app->request->get())) { $filterCondition = $filter->build(); if ($filterCondition === false) { // Serializer would get errors out of it return $filter; } } $query = Post::find(); if ($filterCondition !== null) { $query->andWhere($filterCondition); } return new ActiveDataProvider([ 'query' => $query, ]);
来自 权威指南 https://www.yiiframework.com/doc/guide/2.0/zh-cn/output-data-providers
我感觉此处的ActiveDataFilter只起了一个模型的load作用,只是给query添加了一个where条件,最终还是交给了ActiveDataProvider,我感觉这个filter没啥作用,因为不用它的话,下面的代码也能实现:
public function search($params) { //params是get的参数 $query = Post::find(); $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); $this->load($params); if (!$this->validate()) { return $dataProvider; } return $dataProvider; }
既然都 restful 了,肯定不应该在使用 基于 session 的用户认证模式了,建议你看下权威指南的这一章
https://www.yiiframework.com/doc/guide/2.0/zh-cn/rest-authentication这个过程是这样的:我restful是关掉了session,但是开启速率限制的时候,无法生效,我跟了一下速率限制的代码,发现他要去找用户信息,只有session打开的时候才能找到信息,这就和restful的那个矛盾了。所以我想知道,这个冲突该怎么去解决呢?能指点一下嘛?
- 沃毕尼闷兜率 提出了问题RESTful 速率限制问题