top-think / top-think/think-orm
hasWhere 未自动字段加上表别名
Nobody has claimed this yet.
- #596 by @Hhh0121 — closed without merging
- Dominant language
- PHP
- Stars
- 437
- Forks
- 188
- PR merge metrics
- No merged PRs in 30d
Description
hasWhere 报错,执行代码如下:
$list = Order::with(['orderGoods'])
->where('is_hdfk', '=', 1)
->where('pay_status', '=', 3)
->whereBetweenTime('create_time', $beginTime, $endTime)
->hasWhere('orderGoods', ['goods_id'=> $goodsId])
->select();
报错提示:SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'Order'
根据报错跟踪信息找到执行的 sql 语句结构为:
SELECT `Order`.* FROM `tb_order` JOIN `tb_order_goods` `OrderGoods` ON `Order`.`order_id`=`OrderGoods`.`order_id` WHERE `is_hdfk` = :ThinkBind_1_445930294_ AND `pay_status` = :ThinkBind_2_805866784_ AND `create_time` BETWEEN :ThinkBind_3_493307466_ AND :ThinkBind_4_724524040_ AND `OrderGoods`.`goods_id` = :ThinkBind_5_1775612536_ GROUP BY `Order`.`order_id`
可以看得出问题,是语句中 FROM tb_order 并没有给表加上别名 Order,手动加上 alias('Order') 可解决,但我认为这应该由框架自动加。
另外一个可能出现的Bug:框架使用 JOIN 来实现 hasWhere ,但查询时 Order 模型的 where 条件 is_hdfk、pay_status、create_time 等字段并不会自动加上表名,如果 tb_order_goods 表也有 create_time 字段,sql 语句就报错了。
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how hasWhere builds the JOIN and aliases for the Order and OrderGoods models, using the SQL and reproduction shown in the issue. Check how the base table alias and Order conditions are emitted. Done means the query runs without requiring alias('Order') and conditions remain unambiguous when joined tables share column names.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100