我想用yii2搜索模型创建这个查询
select * from t1 where (title = 'keyword' or content = 'keyword') AND
(category_id = 10 or term_id = 10 )
但是我不知道如何使用orFilterWhere
和andFilterWhere
。
我在搜索模型中的代码:
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])
}
//...
}
但它会创建以下查询:
select * from t1 where title = 'keyword' or content = 'keyword' or
category_id = 10 or term_id = 10
1条答案
按热度按时间niknxzdl1#
首先,您所需的sql语句应该如下所示:
因此,您的查询生成器应该如下所示: