think-orm icon indicating copy to clipboard operation
think-orm copied to clipboard

WhereOr返回的过滤条件为什么是AND

Open notinmood opened this issue 7 months ago • 14 comments

WhereOr过滤条件大家都不使用吗?如下: ` $where[] = ['mobile', 'like', 'thinkphp%']; $whereOr[] = ['name', 'like', '%thinkphp']; Db::name("abstract_logic_testing")->where($where)->whereOr($whereOr)->select();

$actual = Db::name("abstract_logic_testing")->getLastSql(); `

这段代码$actual的结果,不应该是SELECT * FROM m_abstract_logic_testing WHERE ( mobile LIKE 'thinkphp%' ) OR ( name LIKE '%thinkphp' )吗?为什么结果是 SELECT * FROM m_abstract_logic_testing WHERE ( mobile LIKE 'thinkphp%' ) AND ( name LIKE '%thinkphp' )。

这个问题从版本3.0.31就出现了(3.0.30运行这段代码符合预期:是用OR链接的两个条件),到现在4.x版本了都是错的(是用AND连接的两个条件)。

notinmood avatar May 20 '25 02:05 notinmood

你应该直接把mobile也放到whereOr的数组里。

augushong avatar May 20 '25 02:05 augushong

把mobile放在 whereOr里面,也是AND。

notinmood avatar May 20 '25 02:05 notinmood

那就有问题了,先用whereQuery试试,里面一个一个调用whereOr,

augushong avatar May 20 '25 02:05 augushong

1.这么写: $whereOr[] = [['name', 'like', '%thinkphp'],['mobile', 'like', 'thinkphp%']]; Db::name("abstract_logic_testing")->whereOr($whereOr)->select();

$actual = Db::name("abstract_logic_testing")->getLastSql(); 结果是 AND;

2.这么写 $whereOr = [['name', 'like', '%thinkphp'],['mobile', 'like', 'thinkphp%']]; Db::name("abstract_logic_testing")->whereOr($whereOr)->select();

$actual = Db::name("abstract_logic_testing")->getLastSql(); 结果是OR

notinmood avatar May 20 '25 02:05 notinmood

我咋没看出来区别啊。

augushong avatar May 20 '25 02:05 augushong

1中是 $whereOr[],2中是$whereOr;两者差一个中括号。

notinmood avatar May 20 '25 02:05 notinmood

第一个写法不对。没有这种用法。 第一种这样写法才对 $whereOr = [] $whereOr[] = ['name', 'like', '%thinkphp'] $whereOr[] = ['name', 'like', '%thinkphp'] $whereOr[] = ['name', 'like', '%thinkphp']

augushong avatar May 20 '25 02:05 augushong

好,那就回到我最初的问题 $where[] = ['mobile', 'like', 'thinkphp%']; $whereOr[] = ['name', 'like', '%thinkphp']; Db::name("abstract_logic_testing")->where($where)->whereOr($whereOr)->select(); 这段代码,WhereOr条件就是 你说的写法。你感觉这段代码 是AND还是OR

notinmood avatar May 20 '25 02:05 notinmood

这段代码在3.0.30之前,都是OR连接;在3.0.31之后都是AND连接。

notinmood avatar May 20 '25 02:05 notinmood

从我们测试的结果来说,在3.0.30版本之前 ,WhereOr数组跟外部其他Where条件是 Or连接(WhereOr是判断的是其跟外部的关系);3.0.31版本之后,WhereOr判断的是这个数组几个子条件内部的的关系。

notinmood avatar May 20 '25 03:05 notinmood

应该是and.

whereOr方法内的会组成or查询。

最近用是这样用的,但是老版本没印象了。

看下文档有没有使用指导吧。

augushong avatar May 20 '25 03:05 augushong

嗯。感谢你参与讨论。WhereOr判断内部条件之间的关系,更符合“道理”。但小版本更新,直接改变了逻辑流程,还是非常不合适。我们的业务代码,都要从新测试。

notinmood avatar May 20 '25 03:05 notinmood

你的whereOR里面传入的是数组就是内部条件之间是OR 如果是whereOr('name', 'like', '%name%') 这种就是和之前的查询条件OR逻辑

liu21st avatar May 20 '25 03:05 liu21st

你的whereOR里面传入的是数组就是内部条件之间是OR 如果是whereOr('name', 'like', '%name%') 这种就是和之前的查询条件OR逻辑

这么弄,还是非常容易引起开发者混淆的。建议仅保留一种,要么控制内部关系,要么控制外部关系。

notinmood avatar May 20 '25 03:05 notinmood