top-think / top-think/think-orm

一对一关联保存无法传入当前模型主键的问题

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

Bug

一对一关联保存时,关联表数据保存成功后,当前模型主键并未传过去。

版本

image

我的代码
// 主表定义的关联模型
public function admin()
{
    return $this->hasOne(Users::class,'staff_id','userid');
}
// 控制器测试代码
public function test()
{
    // StaffModel->$pk = userid 主键是userid
    $staff = StaffModel::find('xiaomeng6666');
    // 记录写过去了,但是当前模型主键并没有写入到关联模型的staff_id
    return $staff->admin()->save([
        'username' => '11111',
        'mobile' => '13766554444',
        'password' => 'xxxxx'
    ]);
}

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 issue using StaffModel::find('xiaomeng6666') and the hasOne()->save() call shown in the report. Trace the one-to-one association save path and verify whether the current model's userid is available when the related Users record is written. Done means the related record's staff_id contains the current StaffModel primary key.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.