Skip to content

Bug: Mapping Entity's primary key breaks Models save() #9306

@ECode16

Description

@ECode16

PHP Version

7.4

CodeIgniter4 Version

4.4.8

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

cli

Database

Postgres 16

What happened?

When you map primary key in entity it will 'break' isset() on the database name. This causes getIdValue() (Model.php line 542) to return null even when the id exists which causes the save() function to assume everything is insert.

Steps to Reproduce

Entity

class Test extends Entity
{
    protected $datamap = [
        'id' => 'id_test_something'
    ];
    ....
}

Model

class TestModel extends Model {  
	protected $primaryKey = 'id_test_something';
	....
	....
	$this->save($someTestEntity);
}

Expected Output

save() should decide to update.

Anything else?

Changing Model.php line 542 to
if (is_object($row) && $row->{$this->primaryKey} !== null) {
works, but I didn't test it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions