脚本宝典收集整理的这篇文章主要介绍了php – 如何使用Yii 2 ActiveRecord进行IS NULL和IS NOT NULL?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我在seArch()方法中有另一个[gii-generated]搜索模型,其配置如下:
public function search($params) { $query = User::find(); // add condITions that should always apply here $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; } $anDFilterWhere = [ 'id' => $this->id,'status' => $this->status,'role' => $this->role,'created_at' => $this->created_at,'updated_at' => $this->updated_at,'completed_files' => $this->completed_files,// 'activated_at' => null,]; if(!isset($_GET['deleted'])) { $query->where(['deleted_at' => null]); $andFilterWhere['deleted_at'] = null; } else if($_GET['deleted'] === 'true') { $query->where(['not',['deleted_at' => null]]); } // grid filtering conditions $query->andFilterWhere( $andFilterWhere ); $query->andFilterWhere(['like','First_name',$this->username]) ->andFilterWhere(['like','auth_key',$this->auth_key]) ->andFilterWhere(['like','password_hash',$this->password_hash]) ->andFilterWhere(['like','password_reset_token',$this->password_reset_token]) ->andFilterWhere(['like','email',$this->email]) ->andFilterWhere(['like',$this->first_name]) ->andFilterWhere(['like','last_name',$this->last_name]); if($this->activated || $this->activated === "0") { #die(var_dump($this->activated)); if($this->activated === '1') { // this doesn't filter $query->andFilterWhere(['not',['activated_at' => null]]); } else if($this->activated === '0') { // this doesn't either $query->andFilterWhere(['activated_at',null]); } } $dataProvider = new ActiveDataProvider([ 'query' => $query,]); return $dataProvider; }
是的,我在我的班级中设置了激活的属性:
public $activated;
我的rules()方法如下:
public function rules() { return [ [['id','status','role','created_at','updated_at','completed_files'],'integer'],['activated','string'],[['username','deleted_at','completed_files','activated_at'],'safe'],]; }
我试图在search()方法中设置的是根据$activated值过滤字段activated_at(参见上面的代码):
if($this->activated || $this->activated === "0") { #die(VAR_dump($this->activated)); if($this->activated === '1') { // this doesn't filter $query->andFilterWhere(['not',['activated_at' => null]]); } else if($this->activated === '0') { // this doesn't either $query->andFilterWhere(['activated_at',null]); $andFilterWhere['activated_at'] = null; } }
我将它与GridView一起使用 – 除了这个之外,其他每个过滤器都有效.
我在这做错了什么?
Aand如何正确地执行此类查询:
IS NULL something IS NOT NULL something
使用Yii 2的ActiveRecord查询构建器?
编辑:行:if(!isset($_ GET [‘deleted’]))用于其他东西,这是正常的.
->andWhere(['not',['activated_at' => null]])
但是,在执行相关值不为空的情况下执行
以上是脚本宝典为你收集整理的php – 如何使用Yii 2 ActiveRecord进行IS NULL和IS NOT NULL?全部内容,希望文章能够帮你解决php – 如何使用Yii 2 ActiveRecord进行IS NULL和IS NOT NULL?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。