diff --git a/src/GenericModel.php b/src/GenericModel.php index bc35e55..7c0e030 100644 --- a/src/GenericModel.php +++ b/src/GenericModel.php @@ -385,25 +385,23 @@ public static function clearTestEntries(): void * Get a model by id * * @param string $id - * @param bool $update + * @param bool $update always fetch from the database, even if item is cached in the registry. + * @param bool $saveToRegistry save the resulting model to the registry (if enabled) * @return static|null * @throws ModelException */ - public static function get(string $id, bool $update = false): ?static + public static function get(string $id, bool $update = false, bool $saveToRegistry = true): ?static { $registry = ModelRegistry::getInstance(); $driverRegistry = static::getDriverRegistry(); // try to get the model from the registry - if (static::$registry) { + if (static::$registry && !$update) { if ($registryModel = $registry->get(static::class, $id)) { if (!($registryModel instanceof static)) { return null; } - $model = $registryModel; - if (!$update) { - return $model; - } + return $registryModel; } } @@ -437,7 +435,7 @@ public static function get(string $id, bool $update = false): ?static } } - if (static::$registry) { + if (static::$registry && $saveToRegistry) { $registry->save($model); } diff --git a/test/tests/TestDriverTest.php b/test/tests/TestDriverTest.php index bb4180e..1c8f034 100644 --- a/test/tests/TestDriverTest.php +++ b/test/tests/TestDriverTest.php @@ -4,6 +4,8 @@ use Aternos\Model\Driver\DriverRegistry; use Aternos\Model\Driver\Test\TestDriver; +use Aternos\Model\GenericModel; +use Aternos\Model\ModelRegistry; use Aternos\Model\Query\AggregateFunction; use Aternos\Model\Query\Conjunction; use Aternos\Model\Query\CountField; @@ -23,6 +25,7 @@ class TestDriverTest extends TestCase { protected function setUp(): void { + GenericModel::enableRegistry(); $testData = "ABCDEFGHIJ"; foreach (str_split($testData) as $i => $char) { TestModel::addTestEntry([ @@ -727,6 +730,31 @@ public function testSelectDistinctWithAggregateAndGroup(): void $this->assertEquals(3, $result[2]->getField(CountField::COUNT_FIELD)); } + public function testGetWithRegistry(): void + { + $id = "0A"; + $this->assertNull(ModelRegistry::getInstance()->get(TestModel::class, $id)); + + $item = TestModel::get($id); + $this->assertInstanceOf(TestModel::class, $item); + $this->assertEquals($item->getId(), $id); + $this->assertSame($item, ModelRegistry::getInstance()->get(TestModel::class, $id)); + + $item2 = TestModel::get($id, update: true); + $this->assertInstanceOf(TestModel::class, $item2); + $this->assertEquals($item2->getId(), $id); + $this->assertNotSame($item, $item2); + $this->assertSame($item2, ModelRegistry::getInstance()->get(TestModel::class, $id)); + $this->assertNotSame($item, ModelRegistry::getInstance()->get(TestModel::class, $id)); + + $item3 = TestModel::get($id, update: true, saveToRegistry: false); + $this->assertInstanceOf(TestModel::class, $item3); + $this->assertEquals($item3->getId(), $id); + $this->assertNotSame($item, $item3); + $this->assertNotSame($item2, $item3); + $this->assertSame($item2, ModelRegistry::getInstance()->get(TestModel::class, $id)); + } + protected function tearDown(): void { TestModel::clearTestEntries();