Skip to content

Commit

Permalink
fix primary key mapping for the entity in the model
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Dec 7, 2024
1 parent 124a7e6 commit 1c26761
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
8 changes: 5 additions & 3 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@ protected function doErrors()
*/
public function getIdValue($row)
{
if (is_object($row) && isset($row->{$this->primaryKey})) {
if (is_object($row)) {
// Get the raw primary key value of the Entity.
if ($row instanceof Entity) {
if ($row instanceof Entity && $row->{$this->primaryKey} !== null) {
$cast = $row->cast();

// Disable Entity casting, because raw primary key value is needed for database.
Expand All @@ -608,7 +608,9 @@ public function getIdValue($row)
return $primaryKey;
}

return $row->{$this->primaryKey};
if (! $row instanceof Entity && isset($row->{$this->primaryKey})) {
return $row->{$this->primaryKey};
}
}

if (is_array($row) && isset($row[$this->primaryKey])) {
Expand Down
40 changes: 40 additions & 0 deletions tests/system/Models/SaveModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,44 @@ public function testUseAutoIncrementSetToFalseSaveObject(): void
$this->assertSame($insert['key'], $this->model->getInsertID());
$this->seeInDatabase('without_auto_increment', $update);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9306
*/
public function testSaveNewEntityWithMappedPrimaryKey(): void
{
$entity = new class () extends Entity {
protected $name;

Check failure on line 375 in tests/system/Models/SaveModelTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Property class@anonymous/tests/system/Models/SaveModelTest.php:374::$name has no type specified.
protected $attributes = [
'id' => null,
'name' => null,
];
protected $original = [
'id' => null,
'name' => null,
];
protected $datamap = [
'new_kid_in_the_block' => 'id',
];
};

$testModel = new class () extends Model {
protected $table = 'empty';
protected $allowedFields = [
'name',
];
protected $returnType = 'object';
};

$entity->name = 'New';
$this->assertTrue($testModel->save($entity));
$this->seeInDatabase('empty', ['id' => 1, 'name' => 'New']);

$entity->new_kid_in_the_block = 1;
$entity->name = 'Updated';
$this->assertTrue($testModel->save($entity));

$this->seeInDatabase('empty', ['id' => 1, 'name' => 'Updated']);
$testModel->truncate();

Check failure on line 406 in tests/system/Models/SaveModelTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Call to an undefined method class@anonymous/tests/system/Models/SaveModelTest.php:389::truncate().
}
}
2 changes: 1 addition & 1 deletion user_guide_src/source/changelogs/v4.5.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ Bugs Fixed
- **DownloadResponse:** Fixed a bug that prevented setting custom cache headers. We can now also use the ``setCache()`` method.
- **DownloadResponse:** Fixed a bug involving sending a custom "Expires-Disposition" header.
- **Routing:** Fixed a TypeError in `str_replace()` when `Routing::$translateURIDashes` is set to `true` and a route is defined using a closure.

- **Validation:** Fixed a bug where complex language strings were not properly handled.
- **CURLRequest:** Added support for handling proxy responses using HTTP versions other than 1.1.
- **Database:** Fixed a bug that caused ``Postgre\Connection::reconnect()`` method to throw an error when the connection had not yet been established.
- **Model** Fixed a bug that caused the ``Model::getIdValue()`` method to not correctly recognize the primary key in the ``Entity`` object if a data mapping for the primary key was used.

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

0 comments on commit 1c26761

Please sign in to comment.