Skip to content

Commit

Permalink
fix Model::tryLoad usages in Reference::ref
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 29, 2022
1 parent 7e84f10 commit 4e93ded
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Reference/ContainsOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ public function ref(Model $ourModel, array $defaults = []): Model
});
}

$theirModelOrig = $theirModel;
$theirModel = $theirModel->tryLoadOne();

if ($theirModel === null) { // TODO or implement tryRef?
$theirModel = $theirModelOrig->createEntity();
}

return $theirModel;
}
}
5 changes: 5 additions & 0 deletions src/Reference/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,16 @@ public function ref(Model $ourModel, array $defaults = []): Model
$ourValue = $this->getOurFieldValue($ourModel);
$this->assertReferenceValueNotNull($ourValue);

$theirModelOrig = $theirModel;
if ($this->their_field) {
$theirModel = $theirModel->tryLoadBy($this->their_field, $ourValue);
} else {
$theirModel = $theirModel->tryLoad($ourValue);
}

if ($theirModel === null) { // TODO or implement tryRef?
$theirModel = $theirModelOrig->createEntity();
}
}

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

0 comments on commit 4e93ded

Please sign in to comment.