
thinkphp6手动分页时如何处理缺少库存字段的情况
在进行分页查询时,我们可能会遇到查询条件中包含不存在于数据库字段的情况,例如查询有库存的物品但库存数量需要计算得出。本问答探讨了如何在thinkphp6中处理这种情况。
问题描述
原代码如下:
立即学习“PHP免费学习笔记(深入)”;
list($where, $alias, $limit, $order) = $this->queryBuilder();$res = $this->model ->field($this->indexField) ->withJoin($this->withJoinTable, $this->withJoinType) ->alias($alias) ->where($where) ->order($order) ->paginate($limit) ->each(function ($item, $key) { $product_in = Db::name('product_in')->where('product_id', $item['id'])->sum('quantity'); //该物品的入库数量 $product_out = Db::name('product_out')->where('product_id', $item['id'])->sum('quantity'); //该物品的出库数量 $item['stock'] = $product_in - $product_out; //这里是库存计算 });$this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(),]);
解决方案
尽管可以使用子查询在数据库层面解决这个问题,但效率较低。更推荐的方法是:
沟通沟通沟通!与产品经理沟通并讨论忽略此类问题。优化数据表结构。考虑添加库存字段并实时刷新,以便在查询时使用sql过滤。
以上就是ThinkPHP6手动分页:如何处理查询条件中缺少库存字段的情况?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1251193.html
微信扫一扫
支付宝扫一扫