如何在yii2中使用AndWhere中的嵌套条件

时间:2016-07-26 09:42:50

标签: php yii2 yii2-model

在标记为重复问题之前,请仔细阅读。我这里有不同的问题。我想使用yii2搜索模型生成这种查询。

select * from t1 innerjoin t2 on (t1.id = t2.id) where ((t1.price >= '1000' and t1.price <= '5000') OR 
                   ( t2.price >= '1000' and t2.price <= '5000' ))

加入不是问题。主要问题是where子句。我试过这个但没有用。

$query->andFilterWhere([
                     'and',
                     ['>=', 't1.price', $this>start_price],
                     ['<=', 't1.price', $this->end_price]
                 ])
      ->orFilterWhere([
                      'and',
                       ['>=', 't2.price', $this->start_price],
                       ['<=', 't2.price', $this->end_price]
              ]);

1 个答案:

答案 0 :(得分:8)

尝试

$query->andFilterWhere([
    'or',
    [
        'and',
        ['>=', 't1.price', $this>start_price],
        ['<=', 't1.price', $this->end_price]
    ],
    [
        'and',
        ['>=', 't2.price', $this->start_price],
        ['<=', 't2.price', $this->end_price]
    ]
]);