top-think / top-think/think-orm

hasWhere查询bug

Open
#139 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
437
Forks
188
PR merge metrics
No merged PRs in 30d

Description

重现代码

PayProviderGatewayModel::hasWhere('provider', ['status' => 1])
->hasWhere('account', ['status' => 1])
->hasWhere('channel', ['status' => 1])
->whereIn('gateway_group_id', $gateway_group_id)
->where('status', 1)
->where('channel_id', $this->order['channel_id'])
->select();

生成的sql语句是

SELECT PayProviderGatewayModel.* FROM pay_provider_gateway PayProviderGatewayModel INNER JOIN pay_provider PayProviderModel ON PayProviderGatewayModel.provider_id=PayProviderModel.provider_id INNER JOIN pay_provider_account PayProviderAccountModel ON PayProviderGatewayModel.provider_account_id=PayProviderAccountModel.provider_account_id INNER JOIN pay_channel PayChannelModel ON PayProviderGatewayModel.channel_id=PayChannelModel.channel_id WHERE PayProviderModel.status = 1 AND PayProviderAccountModel.status = 1 AND PayChannelModel.status = 1 AND gateway_group_id = 1 AND status = 1 AND channel_id = 3

症状描述

pay_provider_gateway表的status字段查询是需要自动加上alias PayProviderGatewayModel的,但是并没有加,导致了报错。

最后我在代码中新增了
image,解决了这个问题

另一个bug
另外hasWhere如果不放在第一个条件,也会报错,会导致主表pay_provider_gateway没有起别名

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the hasWhere query-generation entry point and reproduce the chained PayProviderGatewayModel query from the issue. Trace alias handling for the pay_provider_gateway status condition and for calls where hasWhere is not first; done means the generated SQL qualifies the main-table fields and preserves the main-table alias without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.