当同一模型也存在 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 Contact
s:
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.
相关文章