問題描述
我正在嘗試在 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.
這篇關于當同一模型也存在 HasMany 關系時,如何更新 HasOne 關系?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!