Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: primary key mapping in the model for the entity #9307

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -15835,12 +15835,6 @@
'count' => 1,
'path' => __DIR__ . '/tests/system/Models/PaginateModelTest.php',
];
$ignoreErrors[] = [
// identifier: method.notFound
'message' => '#^Call to an undefined method class@anonymous/tests/system/Models/SaveModelTest\\.php\\:288\\:\\:truncate\\(\\)\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/system/Models/SaveModelTest.php',
];
$ignoreErrors[] = [
// identifier: property.nonObject
'message' => '#^Cannot access property \\$description on array\\.$#',
Expand Down
10 changes: 6 additions & 4 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})) {
// Get the raw primary key value of the Entity.
if ($row instanceof Entity) {
if (is_object($row)) {
// Get the raw or mapped primary key value of the Entity.
if ($row instanceof Entity && $row->{$this->primaryKey} !== null) {
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
$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
42 changes: 41 additions & 1 deletion tests/system/Models/SaveModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function testSaveNewEntityWithDate(): void

$this->setPrivateProperty($testModel, 'useTimestamps', true);
$this->assertTrue($testModel->save($entity));
$testModel->truncate();
$testModel->db->table('empty')->truncate();
}

public function testInvalidAllowedFieldException(): void
Expand Down 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 string $name;
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->db->table('empty')->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
Loading