Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/GenericModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -437,7 +435,7 @@ public static function get(string $id, bool $update = false): ?static
}
}

if (static::$registry) {
if (static::$registry && $saveToRegistry) {
$registry->save($model);
}

Expand Down
28 changes: 28 additions & 0 deletions test/tests/TestDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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([
Expand Down Expand Up @@ -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();
Expand Down
Loading