yii - Yii2: How to use orWhere in andWhere -
i want create query yii2 search model
select * t1 (title = 'keyword' or content = 'keyword') , (category_id = 10 or term_id = 10 )
but don't know how use orfilterwhere
, andfilterwhere
.
my code in search model:
public function search($params) { $query = app::find(); //... if ($this->keyword) { $query->orfilterwhere(['like', 'keyword', $this->keyword]) ->orfilterwhere(['like', 'content', $this->keyword]) } if ($this->cat) { $query->orfilterwhere(['category_id'=> $this->cat]) ->orfilterwhere(['term_id'=> $this->cat]) } //... }
but creates query:
select * t1 title = 'keyword' or content = 'keyword' or category_id = 10 or term_id = 10
first required sql staement should smth this:
select * t1 ((title '%keyword%') or (content '%keyword%')) , ((category_id = 10) or (term_id = 10))
so query builder should smth this:
public function search($params) { $query = app::find(); ... if ($this->keyword) { $query->andfilterwhere(['or', ['like','title',$this->keyword], ['like','content',$this->keyword]]); } if ($this->cat) { $query->andfilterwhere(['or', ['category_id'=> $this->cat], ['term_id'=> $this->cat]]); }...
Comments
Post a Comment