top-think / top-think/think-orm

一对多withField无效

Open
#212 0 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

public function files()
{
    return $this->belongsToMany(Fileinfo::class, RoomFile::class, 'fileid', 'serial');
}

$query->field('serial,roomname,starttime,endtime')
->with([
    'files' => function (\think\model\Relation $query) {
        $query->withField(['fileid','filename']);
    }
])

依然还是查询了所有字段

SELECT fileinfo.*,pivot.serial AS pivot__serial,pivot.fileid AS pivot__fileid,pivot.showname AS pivot__showname,pivot.type AS pivot__type,pivot.addtime AS pivot__addtime,pivot.suffix AS pivot__suffix,pivot.sort AS pivot__sort,pivot.warmvideo AS pivot__warmvideo FROM fileinfo INNER JOIN roomfile pivot ON pivot.fileid=fileinfo.fileid WHERE ( fileinfo.companyid = 1001219 AND pivot.serial = '21431077062245' ) AND fileinfo.deletetime = 0

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 with the belongsToMany relation and the withField call shown in the issue, then reproduce the generated SQL for the files relation. Trace how the requested fileid and filename fields are applied and verify that the query no longer selects every fileinfo and pivot column.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.