top-think / top-think/think-orm

withJoin和withoutField不能连用吗?

Open
#346 1 comment 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

使用withJion连表查询,希望主表某个字段不要查询,再使用withoutField,会报错,因为withoutField设置的field没有表名。

不使用withoutField时:

SELECT
    `system_admin`.`id`,
    `system_admin`.`auth_id`,
    `system_admin`.`head_img`,
    `system_admin`.`username`,
    `system_admin`.`password`,
    `system_admin`.`phone`,
    `system_admin`.`remark`,
    `system_admin`.`login_num`,
    `system_admin`.`sort`,
    `system_admin`.`status`,
    `system_admin`.`create_time`,
    `system_admin`.`update_time`,
    `system_admin`.`delete_time`,
    `system_admin`.`nickname`,
    `system_admin`.`email`,
    `system_admin`.`wechat_official_account_openid`,
    `system_admin`.`notifications_setting`,
    `system_admin`.`uid`,
    `systemAuth`.`id` AS `systemAuth__id`,
    `systemAuth`.`title` AS `systemAuth__title`
FROM
    `ea_system_admin` `system_admin`
    LEFT JOIN `ea_system_auth` `systemAuth` ON `system_admin`.`auth_id` = `systemAuth`.`id`
WHERE
    `system_admin`.`delete_time` IS NULL
ORDER BY
    `system_admin`.`sort` DESC,
    `system_admin`.`id` DESC
LIMIT
    0, 15

使用时,除了正确的字段,后面还多了一批字段:

SELECT
    `system_admin`.`id`,
    `system_admin`.`auth_id`,
    `system_admin`.`head_img`,
    `system_admin`.`username`,
    `system_admin`.`password`,
    `system_admin`.`phone`,
    `system_admin`.`remark`,
    `system_admin`.`login_num`,
    `system_admin`.`sort`,
    `system_admin`.`status`,
    `system_admin`.`create_time`,
    `system_admin`.`update_time`,
    `system_admin`.`delete_time`,
    `system_admin`.`nickname`,
    `system_admin`.`email`,
    `system_admin`.`wechat_official_account_openid`,
    `system_admin`.`notifications_setting`,
    `system_admin`.`uid`,
    `systemAuth`.`id` AS `systemAuth__id`,
    `systemAuth`.`title` AS `systemAuth__title`,
    `id`,
    `auth_id`,
    `head_img`,
    `username`,
    `password`,
    `phone`,
    `remark`,
    `login_num`,
    `sort`,
    `status`,
    `create_time`,
    `update_time`,
    `delete_time`,
    `nickname`,
    `email`,
    `wechat_official_account_openid`,
    `notifications_setting`,
    `uid`
FROM
    `ea_system_admin` `system_admin`
    LEFT JOIN `ea_system_auth` `systemAuth` ON `system_admin`.`auth_id` = `systemAuth`.`id`
WHERE
    `system_admin`.`delete_time` IS NULL
ORDER BY
    `system_admin`.`sort` DESC,
    `system_admin`.`id` DESC
LIMIT
    0, 15

会导致报错:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous

无论在withoutField中怎样的写法都不行:

->withoutField('system_admin.password')
->withoutField('password')

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

Reproduce the reported query using withJoin and withoutField, comparing the two generated SELECT statements shown in the issue. Trace how both options build the selected field list; done means excluding the requested main-table field without adding duplicate unqualified columns, so the joined query executes without the ambiguous-column error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.