Skip to content

Commit

Permalink
Merge pull request #8614 from kenjis/fix-model-save-empty-id
Browse files Browse the repository at this point in the history
fix: Model::shouldUpdate() logic
  • Loading branch information
kenjis authored Mar 15, 2024
2 parents e55d478 + ee1eb7c commit a87fd14
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ protected function shouldUpdate($row): bool
{
$id = $this->getIdValue($row);

return ! ($id === null || $id === []);
return ! ($id === null || $id === [] || $id === '');
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Models/SaveModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ public function testSaveNewRecordObject(): void
$this->seeInDatabase('job', ['name' => 'Magician']);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8613
*/
public function testSaveNewRecordArrayWithEmptyStringId(): void
{
$this->createModel(JobModel::class);

$data = [
'id' => '',
'name' => 'Magician',
'description' => 'Makes peoples things dissappear.',
];

$this->model->save($data);

$this->seeInDatabase('job', ['name' => 'Magician']);
}

public function testSaveNewRecordArray(): void
{
$this->createModel(JobModel::class);
Expand Down

0 comments on commit a87fd14

Please sign in to comment.