gpt4 book ai didi

php - 如何在 YII2 ActiveRecord 查询中说 (a AND b) OR (c AND d)?

转载 作者:可可西里 更新时间:2023-11-01 00:42:19 26 4
gpt4 key购买 nike

我在使用 YII2 的 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 )

问题:

在 YII2 中,where() 方法不允许“嵌套”查询,这就是我卡住的地方。 YII2 只允许说简单的 AND .. OR 构造,绝不允许将一组 AND 放在一起。

最佳答案

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

Demo::find()->where(['or', ['a' => 1, 'b' => 2], ['a' => 3, 'b' => 4]]);

官方文档中有一些例子here .

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

Demo::find()->where([
'or',
['and', ['not in', 'a' => [1, 2]], ['not in', 'b' => [3, 4]]],
['a' => [5, 6]], 'b' => [7, 8]],
]);

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

关于php - 如何在 YII2 ActiveRecord 查询中说 (a AND b) OR (c AND d)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31859630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com