One - One Code All

Blog Content

thinkphp8单次访问需要多次操作数据库导致各种查询条件累加问题,newQuery

PHP   2023-07-02 22:30:47

thinkphp8单次访问需要多次操作数据库导致各种查询条件累加问题。


项目场景:

需要循环删除数据,


问题描述:

发现sql中where id=1 and where id=2 and。。。。


原因分析:

每次数据库操作,thinkphp会在db中option中存入各种数据:data、order、join、where。。。。,但是这个二货每次查询完毕不清空!导致第二次调用同一对象的相关操作条件会叠加!


解决方案:

用getOptions函数打印事实数组,找到问题数据存储项;然后用setOption清空即可;setOption支持链式调用;


或者使用:newQuery

$data1 = $this->weav_material_price_db->newQuery()->where(['material_name'=>'DE75'])->value('material_price');


上一篇:thinkphp6分页传递参数
下一篇:thinkphp6测试有效,前端模板常用方法

The minute you think of giving up, think of the reason why you held on so long.