Skip to content

Commit 4e93ded

Browse files
committed
fix Model::tryLoad usages in Reference::ref
1 parent 7e84f10 commit 4e93ded

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/Reference/ContainsOne.php

+5
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,13 @@ public function ref(Model $ourModel, array $defaults = []): Model
9494
});
9595
}
9696

97+
$theirModelOrig = $theirModel;
9798
$theirModel = $theirModel->tryLoadOne();
9899

100+
if ($theirModel === null) { // TODO or implement tryRef?
101+
$theirModel = $theirModelOrig->createEntity();
102+
}
103+
99104
return $theirModel;
100105
}
101106
}

src/Reference/HasOne.php

+5
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,16 @@ public function ref(Model $ourModel, array $defaults = []): Model
9191
$ourValue = $this->getOurFieldValue($ourModel);
9292
$this->assertReferenceValueNotNull($ourValue);
9393

94+
$theirModelOrig = $theirModel;
9495
if ($this->their_field) {
9596
$theirModel = $theirModel->tryLoadBy($this->their_field, $ourValue);
9697
} else {
9798
$theirModel = $theirModel->tryLoad($ourValue);
9899
}
100+
101+
if ($theirModel === null) { // TODO or implement tryRef?
102+
$theirModel = $theirModelOrig->createEntity();
103+
}
99104
}
100105

101106
// their model will be reloaded after saving our model to reflect changes in referenced fields

0 commit comments

Comments
 (0)