Skip to content

Commit 3236e60

Browse files
committed
Model::tryLoad returns null when not found
1 parent d650c11 commit 3236e60

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"friendsofphp/php-cs-fixer": "^3.0",
5151
"johnkary/phpunit-speedtrap": "^3.3",
5252
"phpstan/extension-installer": "^1.1",
53-
"phpstan/phpstan": "^1.0",
53+
"phpstan/phpstan": "^1.6",
5454
"phpstan/phpstan-deprecation-rules": "^1.0",
5555
"phpunit/phpunit": "^9.5.5"
5656
},

src/Model.php

+21-15
Original file line numberDiff line numberDiff line change
@@ -1234,9 +1234,9 @@ private function remapIdLoadToPersistence($id)
12341234
/**
12351235
* @param mixed $id
12361236
*
1237-
* @return $this
1237+
* @return ($fromTryLoad is true ? $this|null : $this)
12381238
*/
1239-
private function _loadThis(bool $isTryLoad, $id)
1239+
private function _loadThis(bool $fromTryLoad, $id)
12401240
{
12411241
$this->assertIsEntity();
12421242
if ($this->isLoaded()) {
@@ -1250,21 +1250,27 @@ private function _loadThis(bool $isTryLoad, $id)
12501250
return $this;
12511251
}
12521252
$dataRef = &$this->getDataRef();
1253-
$dataRef = $this->persistence->{$isTryLoad ? 'tryLoad' : 'load'}($this->getModel(), $this->remapIdLoadToPersistence($id));
1254-
if ($isTryLoad && $dataRef === null) {
1255-
$dataRef = [];
1256-
$this->unload();
1257-
} else {
1258-
if ($this->id_field) {
1259-
$this->setId($this->getId());
1260-
}
1253+
$dataRef = $this->persistence->{$fromTryLoad ? 'tryLoad' : 'load'}($this->getModel(), $this->remapIdLoadToPersistence($id));
12611254

1262-
$ret = $this->hook(self::HOOK_AFTER_LOAD);
1263-
if ($ret === false) {
1264-
$this->unload();
1265-
} elseif (is_object($ret)) {
1266-
return $ret; // @phpstan-ignore-line
1255+
if ($dataRef === null) {
1256+
// $fromTryLoad is true here
1257+
1258+
return null;
1259+
}
1260+
1261+
if ($this->id_field) {
1262+
$this->setId($this->getId());
1263+
}
1264+
1265+
$ret = $this->hook(self::HOOK_AFTER_LOAD);
1266+
if ($ret === false) {
1267+
if ($fromTryLoad) {
1268+
return null;
12671269
}
1270+
1271+
$this->unload();
1272+
} elseif (is_object($ret)) {
1273+
return $ret; // @phpstan-ignore-line
12681274
}
12691275

12701276
return $this;

0 commit comments

Comments
 (0)