問題描述
我正在嘗試在 Eloquent 中定義相同的兩個(gè)模型之間的 HasMany 和 HasOne 關(guān)系.
我的Organization
類有很多Contact
:
公共函數(shù)contacts(){返回 $this->hasMany(Contact::class);}
同樣,我的 Contact
類反映了這種關(guān)系:
公共函數(shù)組織(){返回 $this->belongsTo(Organization::class);}
而且,每個(gè)組織
都有一個(gè)主要"聯(lián)系人
.我正在使用表列 organizations.primary_contact_id
來確定哪個(gè):
公共函數(shù)primaryContact(){返回 $this->hasOne(Contact::class, 'id', 'primary_contact_id');}
從這里開始,我被卡住了.Contact
中的反向關(guān)系已經(jīng)存在,所以我寫了另一個(gè)我認(rèn)為可以解決問題的函數(shù),計(jì)算如果我更新了父表中的值,Eloquent 自然會(huì)在contacts 表中獲取相應(yīng)的記錄,因?yàn)槲叶x了關(guān)系:
/*** @param AppContact*/公共函數(shù) setPrimaryContact($contact){$this->primary_contact_id = $contact->id;$this->save;}
但它沒有:
<預(yù)><代碼>>>>$org = 組織::查找(17)=>應(yīng)用組織 {#2923編號(hào):17,name: "測試組織",primary_contact_id: 33,}>>>$alice= $org->primaryContact=>應(yīng)用聯(lián)系{#2938編號(hào):33,組織 ID:17,fname: "愛麗絲",lname: "方丈",}>>>$bob = 聯(lián)系人::查找(34)=>應(yīng)用聯(lián)系{#2939編號(hào):34,組織 ID:17,fname: "鮑勃",lname: "面包師",}>>>$org->setPrimaryContact($bob)=>空值>>>$org=>應(yīng)用組織 {#2923編號(hào):17,name: "測試組織",primary_contact_id: 34,主要聯(lián)系人:AppContact {#2938編號(hào):33,組織 ID:17,fname: "愛麗絲",lname: "方丈",},}您可以看到 setPrimaryContact($bob)
執(zhí)行得很好,因?yàn)?primary_contact_id
已更新為 Bob 的 id
,但是 primaryContact代碼> 仍然列出 Alice.
為什么 primaryContact
沒有返回正確的對(duì)象?
- 您的
setPrimaryContact
方法不會(huì)更新您的表,因?yàn)槟{(diào)用的是$this->save
,而不是$this->save()
,save
是一個(gè)方法 - 在
$org->setPrimaryContact($bob)
之后,你應(yīng)該調(diào)用$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.
這篇關(guān)于當(dāng)同一模型也存在 HasMany 關(guān)系時(shí),如何更新 HasOne 關(guān)系?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!