即使数据库中的数据正确,Laravel雄辩的关系对象也会过时 [英] Laravel Eloquent relationship object stale even though data correct in database
问题描述
我正在使用Laravel 5.7,并且具有一对一两种雄辩模型之间的一种关系.
I'm using Laravel 5.7 and have a one-to-one relationship between 2 eloquent models.
我有一个运行良好的简单函数,正确的值一直保存到数据库中:
I have this simple function that works well, and the correct values persist to the database:
public function saveMarketingOriginInfo(Contact $contact, $data) {
$contact->marketingOrigin()->create($data);
$this->makeOtherChangesByReference($contact->marketingOrigin);
$contact->marketingOrigin->save();
return $contact->marketingOrigin;
}
但是,当为它编写功能测试时,我注意到它返回的对象是陈旧的(其属性中没有正确的值).
However, when writing a functional test for it, I noticed that the object that it returns is stale (doesn't have the correct values in its properties).
仅当我将return语句更改为return \App\Models\MarketingOrigin::find($contact->id);
时,我的测试才通过.
My tests only pass if I change the return statement to return \App\Models\MarketingOrigin::find($contact->id);
.
(MarketingOrigin使用"contact_id"作为主键.)
(MarketingOrigin uses 'contact_id' as primary key.)
我做错了什么?
如何在不进行数据库读取查询(find()
)的情况下返回刚刚保存在上一行($contact->marketingOrigin->save();
)中的相同对象?
How can I return the same object that was just saved in the previous line ($contact->marketingOrigin->save();
) without making a database read query (find()
)?
protected $table = 'marketing_origins';//MarketingOrigin class
protected $primaryKey = 'contact_id';
protected $guarded = [];
public function contact() {
return $this->belongsTo('App\Models\Contact');
}
测试:
public function testSaveMarketingOriginInfo() {
$helper = new \App\Helpers\SignupHelper();
$contactId = 92934;
$contact = factory(\App\Models\Contact::class)->create(['id' => $contactId]);
$leadMagnetType = 'LMT';
$audience = 'a60907';
$hiddenMktgFields = [
'audience' => $audience,
'leadMagnetType' => $leadMagnetType
];
$result = $helper->saveMarketingOriginInfo($contact, $hiddenMktgFields);
$this->assertEquals($result->contact_id, $contactId, 'contact_id did not get saved');
$this->assertEquals($result->campaignId, '6075626793661');
$this->assertEquals($result->leadMagnetType, $leadMagnetType);
$marketingOrigin = \App\Models\MarketingOrigin::findOrFail($contactId);
$this->assertEquals($marketingOrigin->adsetId, '6088011244061');
$this->assertEquals($marketingOrigin->audience, $audience);
$this->assertEquals($marketingOrigin, $result, 'This is the assertion that fails; some properties of the object are stale');
}
推荐答案
这是因为尚未加载关系.
This is because the relationship has not been loaded yet.
您可以尝试$contact->load('marketingOrigin');
渴望加载关系:
You could try $contact->load('marketingOrigin');
to eager load the relationship:
public function saveMarketingOriginInfo(Contact $contact, $data) {
$contact->marketingOrigin()->create($data);
$this->makeOtherChangesByReference($contact->marketingOrigin);
$contact->marketingOrigin->save();
$contact->load('marketingOrigin'); // <---- eager load the relationship
return $contact->marketingOrigin;
}
这篇关于即使数据库中的数据正确,Laravel雄辩的关系对象也会过时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!