Skip to content

Commit 21f92b2

Browse files
committed
Model::tryLoad returns null when not found
1 parent d650c11 commit 21f92b2

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
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

+9-6
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 ? $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,17 +1250,20 @@ 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();
1253+
$dataRef = $this->persistence->{$fromTryLoad ? 'tryLoad' : 'load'}($this->getModel(), $this->remapIdLoadToPersistence($id));
1254+
if ($fromTryLoad && $dataRef === null) {
1255+
return null;
12571256
} else {
12581257
if ($this->id_field) {
12591258
$this->setId($this->getId());
12601259
}
12611260

12621261
$ret = $this->hook(self::HOOK_AFTER_LOAD);
12631262
if ($ret === false) {
1263+
if ($fromTryLoad) {
1264+
return null;
1265+
}
1266+
12641267
$this->unload();
12651268
} elseif (is_object($ret)) {
12661269
return $ret; // @phpstan-ignore-line

0 commit comments

Comments
 (0)