top-think / top-think/think-orm

主键问题

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

多对多关联,插入数据,主键没有设置成功
在User模型onBeforeInsert里主动设置了id
$structure = Structure::find($structureId);
$structure->users()->save($userData);
user表插入数据成功,但是id插入为空。
如果使用User::create($userData),插入数据,id设置成功

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 case using the User model's onBeforeInsert hook, Structure::find($structureId), and the structure's users()->save($userData) path. Compare it with User::create($userData) and trace where the primary key is handled during the many-to-many save. Done means the association path persists the explicitly assigned User id, with a regression test covering both paths.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.