top-think / top-think/think-orm

一对多关联多层访问时数据异常

Open
#425 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

【简述】
A、B两个模型一对多关联,直接访问B->A->Bs时,得到的结果不符合预期

【环境】
[MySQL]
表 User ,通过 team_id 关联一个 Team
表 Team

[model]
模型 User,定义关联:Team -> belongTo(Team::class, 'team_id', 'id')
模型 Team,定义关联:Users -> hasMany(User::class, 'team_id, 'id')

【复现场景】
[controller]
$user = User::find(1);
dump($user->team->users); // 得到的是$user自身,预期应该得到 user 所在 team 的所有 users
// 这里采用 数组方式 $user['team']['users'] 也是一样的结果

$user = User::with(['team'=>['users']])->find(2);
dump($user->team->users); // 得到的结果是正确的 think\model\Collection

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 User and Team model relationship definitions and compare lazy access through user->team->users with the eager-loaded with(['team'=>['users']]) case shown in the controller. Done means both access paths return all users belonging to the user's team rather than only the original user.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.