Skip to content

Commit 7a3ef88

Browse files
committed
fix: save() does not determine that data with an empty string id is a new record
1 parent bcb88b4 commit 7a3ef88

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

system/BaseModel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ protected function shouldUpdate($row): bool
731731
{
732732
$id = $this->getIdValue($row);
733733

734-
return ! ($id === null || $id === []);
734+
return ! ($id === null || $id === [] || $id === '');
735735
}
736736

737737
/**

tests/system/Models/SaveModelTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ public function testSaveNewRecordObject(): void
4242
$this->seeInDatabase('job', ['name' => 'Magician']);
4343
}
4444

45+
/**
46+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8613
47+
*/
48+
public function testSaveNewRecordArrayWithEmptyStringId(): void
49+
{
50+
$this->createModel(JobModel::class);
51+
52+
$data = [
53+
'id' => '',
54+
'name' => 'Magician',
55+
'description' => 'Makes peoples things dissappear.',
56+
];
57+
58+
$this->model->save($data);
59+
60+
$this->seeInDatabase('job', ['name' => 'Magician']);
61+
}
62+
4563
public function testSaveNewRecordArray(): void
4664
{
4765
$this->createModel(JobModel::class);

0 commit comments

Comments
 (0)