Skip to content

Commit 405435f

Browse files
authored
Merge pull request #7042 from michalsn/fix/noAutoIncrementWithObject
fix: save object when useAutoIncrement is disabled
2 parents a2f09ca + e9b627f commit 405435f

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

system/Model.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,12 @@ public function insert($data = null, bool $returnID = true)
685685
}
686686
}
687687

688-
if ($this->useAutoIncrement === false && isset($data[$this->primaryKey])) {
689-
$this->tempPrimaryKeyValue = $data[$this->primaryKey];
688+
if ($this->useAutoIncrement === false) {
689+
if (is_array($data) && isset($data[$this->primaryKey])) {
690+
$this->tempPrimaryKeyValue = $data[$this->primaryKey];
691+
} elseif (is_object($data) && isset($data->{$this->primaryKey})) {
692+
$this->tempPrimaryKeyValue = $data->{$this->primaryKey};
693+
}
690694
}
691695

692696
$this->escape = $this->tempData['escape'] ?? [];

tests/system/Models/SaveModelTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,27 @@ public function testUseAutoIncrementSetToFalseSave(): void
322322
$this->assertSame($insert['key'], $this->model->getInsertID());
323323
$this->seeInDatabase('without_auto_increment', $update);
324324
}
325+
326+
public function testUseAutoIncrementSetToFalseSaveObject(): void
327+
{
328+
$this->createModel(WithoutAutoIncrementModel::class);
329+
330+
$insert = [
331+
'key' => 'some_random_key',
332+
'value' => 'some value',
333+
];
334+
$this->model->save((object) $insert);
335+
336+
$this->assertSame($insert['key'], $this->model->getInsertID());
337+
$this->seeInDatabase('without_auto_increment', $insert);
338+
339+
$update = [
340+
'key' => 'some_random_key',
341+
'value' => 'some different value',
342+
];
343+
$this->model->save((object) $update);
344+
345+
$this->assertSame($insert['key'], $this->model->getInsertID());
346+
$this->seeInDatabase('without_auto_increment', $update);
347+
}
325348
}

0 commit comments

Comments
 (0)