Skip to content

Commit 63e9ebd

Browse files
authored
fix: setting created_at field in Model::replace() method (#9693)
1 parent af194bf commit 63e9ebd

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

system/BaseModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,9 @@ public function replace(?array $row = null, bool $returnSQL = false)
12201220
return false;
12211221
}
12221222

1223-
$row = $this->setUpdatedField((array) $row, $this->setDate());
1223+
$row = (array) $row;
1224+
$row = $this->setCreatedField($row, $this->setDate());
1225+
$row = $this->setUpdatedField($row, $this->setDate());
12241226

12251227
return $this->doReplace($row, $returnSQL);
12261228
}

tests/system/Models/ReplaceModelTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ public function testReplaceRespectsUseTimestamps(): void
3838
$data['country'] = 'UK';
3939

4040
$sql = $this->model->replace($data, true);
41+
$this->assertStringNotContainsString('created_at', (string) $sql);
4142
$this->assertStringNotContainsString('updated_at', (string) $sql);
4243

4344
$this->model = $this->createModel(UserModel::class);
4445
$this->setPrivateProperty($this->model, 'useTimestamps', true);
4546
$sql = $this->model->replace($data, true);
47+
$this->assertStringContainsString('created_at', (string) $sql);
4648
$this->assertStringContainsString('updated_at', (string) $sql);
4749
}
4850
}

user_guide_src/source/changelogs/v4.6.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Bugs Fixed
3333
- **Database:** Fixed a bug in ``Database::connect()`` which was causing to store non-shared connection instances in shared cache.
3434
- **Database:** Fixed a bug in ``Connection::getFieldData()`` for ``SQLSRV`` and ``OCI8`` where extra characters were returned in column default values (specific to those handlers), instead of following the convention used by other drivers.
3535
- **Forge:** Fixed a bug in ``Postgre`` and ``SQLSRV`` where changing a column's default value using ``Forge::modifyColumn()`` method produced incorrect SQL syntax.
36+
- **Model:** Fixed a bug in ``Model::replace()`` where ``created_at`` field (when available) wasn't set correctly.
3637

3738
See the repo's
3839
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)