Php 在yii2activerecord查询中如何表示(a和b)或(c和d)?

Php 在yii2activerecord查询中如何表示(a和b)或(c和d)?,php,activerecord,orm,yii2,Php,Activerecord,Orm,Yii2,我在使用Yi2的ORM时遇到了困难,它没有记录一些非常简单的典型SQL案例,例如 伪代码 SELECT * FROM table WHERE (a=1 AND b=2) OR (a=3 AND b=4) 我所尝试的: // should represent the commented logic, but does not Demo::find() ->where(...) // ( condition one ->andWhere(...) // AND cond

我在使用Yi2的ORM时遇到了困难,它没有记录一些非常简单的典型SQL案例,例如

伪代码

SELECT * FROM table WHERE (a=1 AND b=2) OR (a=3 AND b=4)
我所尝试的:

// should represent the commented logic, but does not
Demo::find()
    ->where(...) // ( condition one
    ->andWhere(...) // AND condition two )
    ->orWhere(...) // OR (!) ( condition three
    ->andWhere(...) // AND condition four )
问题:

// should represent the commented logic, but does not
Demo::find()
    ->where(...) // ( condition one
    ->andWhere(...) // AND condition two )
    ->orWhere(...) // OR (!) ( condition three
    ->andWhere(...) // AND condition four )

在YII2中,where()-方法不允许“嵌套”查询,这正是我遇到的问题。YII2只允许说简单和。。或者构造,永远不要把一组AND放在一起。

你可以这样做

Demo::find()->where('a=1 and b=2 or f=3')->one();
Demo::find()
    ->where('(a = :a AND b = :b)', [
        ':a' => 'bla',
        ':b' => 'bla',
    ])
    ->orWhere('(c = :c AND d = :d)', [
        ':c' => 'bla',
        ':d' => 'bla',
    ]);
这些文档很好地解释了where选项的可能性。包括如何使用子查询

说明说明了如何以与上面所示不同的方式创建集合。

where()
方法允许嵌套条件:

Demo::find()->where(['or', ['a' => 1, 'b' => 2], ['a' => 3, 'b' => 4]]);
官方文件中有一些例子

更复杂的示例(如您在评论中所问):


另一个例子可以在类似的问题中找到。

谢谢,回答得很好,但不幸的是,这确实支持
x=>1
语法,但不支持not、not in等构造。