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
5 changes: 3 additions & 2 deletions src/GenericModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,16 @@ public function reload(): static
/**
* Save the model changes
*
* @param bool $saveToRegistry save the model to the registry (if enabled)
* @return bool
*/
public function save(): bool
public function save(bool $saveToRegistry = true): bool
{
// new model, generate id and save in registry
if (!$this->getId()) {
$this->generateId();

if (static::$registry) {
if (static::$registry && $saveToRegistry) {
ModelRegistry::getInstance()->save($this);
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/tests/TestDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,25 @@ public function testGetWithRegistry(): void
$this->assertSame($item2, ModelRegistry::getInstance()->get(TestModel::class, $id));
}

public function testSaveWithRegistry(): void
{
$item = new TestModel();
$this->assertNull($item->getId());

$item->save(false);
$this->assertNull(ModelRegistry::getInstance()->get(TestModel::class, $item->id));

// Models are only added to the registry when they are saved for the first time
$item->save();
$this->assertNull(ModelRegistry::getInstance()->get(TestModel::class, $item->id));

// Reset id to create a new model
$item->setId(null);

$item->save();
$this->assertSame($item, ModelRegistry::getInstance()->get(TestModel::class, $item->id));
}

protected function tearDown(): void
{
TestModel::clearTestEntries();
Expand Down
Loading