当同一模型也存在 HasMany 关系时,如何更新 HasOne 关系?

How to update a HasOne relationship when a HasMany relationship also exists with the same model?(当同一模型也存在 HasMany 关系时,如何更新 HasOne 关系?)
本文介绍了当同一模型也存在 HasMany 关系时,如何更新 HasOne 关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在 Eloquent 中定义相同的两个模型之间的 HasMany 和 HasOne 关系.

我的Organization类有很多Contact:

公共函数contacts(){返回 $this->hasMany(Contact::class);}

同样,我的 Contact 类反映了这种关系:

公共函数组织(){返回 $this->belongsTo(Organization::class);}

而且,每个组织都有一个主要"联系人.我正在使用表列 organizations.primary_contact_id 来确定哪个:

公共函数primaryContact(){返回 $this->hasOne(Contact::class, 'id', 'primary_contact_id');}

从这里开始,我被卡住了.Contact 中的反向关系已经存在,所以我写了另一个我认为可以解决问题的函数,计算如果我更新了父表中的值,Eloquent 自然会在contacts 表中获取相应的记录,因为我定义了关系:

/*** @param AppContact*/公共函数 setPrimaryContact($contact){$this->primary_contact_id = $contact->id;$this->save;}

但它没有:

<预><代码>>>>$org = 组织::查找(17)=>应用组织 {#2923编号:17,name: "测试组织",primary_contact_id: 33,}>>>$alice= $org->primaryContact=>应用联系{#2938编号:33,组织 ID:17,fname: "爱丽丝",lname: "方丈",}>>>$bob = 联系人::查找(34)=>应用联系{#2939编号:34,组织 ID:17,fname: "鲍勃",lname: "面包师",}>>>$org->setPrimaryContact($bob)=>空值>>>$org=>应用组织 {#2923编号:17,name: "测试组织",primary_contact_id: 34,主要联系人:AppContact {#2938编号:33,组织 ID:17,fname: "爱丽丝",lname: "方丈",},}

您可以看到 setPrimaryContact($bob) 执行得很好,因为 primary_contact_id 已更新为 Bob 的 id,但是 primaryContact 仍然列出 Alice.

为什么 primaryContact 没有返回正确的对象?

解决方案

  • 您的 setPrimaryContact 方法不会更新您的表,因为您调用的是 $this->save,而不是 $this->save(), save 是一个方法
  • $org->setPrimaryContact($bob)之后,你应该调用$org->primaryContact->refresh() 以获取更新的记录.

I'm trying to define both a HasMany and HasOne relationship between the same two models in Eloquent.

My Organization class has many Contacts:

public function contacts()
{
    return $this->hasMany(Contact::class);
}

And likewise, my Contact class reflects this relationship:

public function organization()
{
    return $this->belongsTo(Organization::class);
}

But also, each Organization has exactly one "primary" Contact. I am using a table column organizations.primary_contact_id to identify which one:

public function primaryContact()
{
    return $this->hasOne(Contact::class, 'id', 'primary_contact_id');
}

From here, I'm stuck. The reverse relationship in Contact already exists, so I wrote another function I thought would do the trick, figuring if I updated the value in the parent table, Eloquent would naturally fetch the corresponding record in the contacts table since I defined the relationship:

/**
 * @param AppContact
 */
public function setPrimaryContact($contact)
{
    $this->primary_contact_id = $contact->id;
    $this->save;
}

But it doesn't:

>>> $org = Organization::find(17)
=> AppOrganization {#2923
     id: 17,
     name: "Test Org",
     primary_contact_id: 33,
   }
>>> $alice= $org->primaryContact
=> AppContact {#2938
     id: 33,
     organization_id: 17,
     fname: "Alice",
     lname: "Abbot",
   }
>>> $bob = Contact::find(34)
=> AppContact {#2939
     id: 34,
     organization_id: 17,
     fname: "Bob",
     lname: "Baker",
   }
>>> $org->setPrimaryContact($bob)
=> null
>>> $org
=> AppOrganization {#2923
     id: 17,
     name: "Test Org",
     primary_contact_id: 34,
     primaryContact: AppContact {#2938
       id: 33,
       organization_id: 17,
       fname: "Alice",
       lname: "Abbot",
     },
   }

You can see setPrimaryContact($bob) executed fine, as primary_contact_id got updated to Bob's id, but primaryContact still lists Alice.

Why is primaryContact not returning the correct object?

解决方案

  • Your setPrimaryContact method won't update your table, because you call $this->save, not $this->save(), save is a method
  • After $org->setPrimaryContact($bob), you should call $org-> primaryContact->refresh() to get the updated record.

这篇关于当同一模型也存在 HasMany 关系时,如何更新 HasOne 关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)