Skip to content

Dev: Make getInsertID() more consistent with save() #9233

Open
@evansharp

Description

@evansharp

PHP Version

8.2, 8.3

CodeIgniter4 Version

latest

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

macOS, Linux

Which server did you use?

fpm-fcgi

Database

MaraiaDB

What happened?

Not exactly a bug, but not really a FR either. See this forum thread: https://forum.codeigniter.com/showthread.php?tid=91800&pid=421782#pid421782

Essentially I'm offering to PR a change to \System\Model::update() that would set an instance's $insertID property the same way \System\Model::insert() does so that the behaviour of save() is more consistent.

Currently \System\Model::getInsertID() only returns a value if a save() does an insert. The logic needed to handle that works against the elegance provided by save().

Steps to Reproduce

Use \System\Model::save()

Expected Output

Best case, \System\Model::getInsertID() will return the "affected ID" after \System\Model::save() regardless of which action occurred.

Anything else?

I'm stoked contribute to core, but I didn't want to spend the time on a PR if there is no desire to change this behaviour. Please advise.

I can write the update to the docs too if I'm pointed in the right direction to do so.

Activity

added
bugVerified issues on the current code behavior or pull requests that will fix them
on Oct 22, 2024
michalsn

michalsn commented on Oct 22, 2024

@michalsn
Member

IMO this may be quite problematic.

The insertID is closely related to the insert query - using the same variable for the update query can be confusing.

Others' code may rely on the fact that insertID is only populated if a new record is added to the table.

So this "small" change may break many things for others.

removed
bugVerified issues on the current code behavior or pull requests that will fix them
on Oct 22, 2024
changed the title [-]Bug: Make getInsertID() more consistent with save()[/-] [+]Dev: Make getInsertID() more consistent with save()[/+] on Oct 22, 2024
kenjis

kenjis commented on Oct 25, 2024

@kenjis
Member

getInsertID() returns inserted row’s primary key.

You can retrieve the last inserted row’s primary key using the getInsertID() method.
https://codeigniter.com/user_guide/models/model.html

It is not getUpdateID().
The method name is very important. I don't think getInsertID() returns updated row's primary key.

Also, this proposal may break existing apps. So it is not acceptable.

You should show use cases why you need this, and it seems better to add a new method for it.

evansharp

evansharp commented on Oct 27, 2024

@evansharp
ContributorAuthor

Also, this proposal may break existing apps. So it is not acceptable.

Fair point.

You should show use cases why you need this

I tried to above; let me be even more clear.

If save() is a convenience method to allow writing a controller method that does not need to figure out if it is saving a new record or updating an existing one, the current behaviour of getInsertID() in many cases negates that convenience.

After either operation, we typically need the primary key of the new/updated record in order to do something else with it. Having to perform logic to figure out which model method to use to get that ID means we might as well not have used save() in the first place; and instead would use that same logic to call insert() or update() ourselves.

it seems better to add a new method for it

Yes, that could also create the consistency I'm talking about. How would we feel about "\System\Model::getSavedID()" that does the differential logic I describe above for the controller?

mbnl

mbnl commented on Mar 25, 2025

@mbnl

Yes, in cases where multiple insertions and updates are performed across related tables, a method like getSavedID() is definitely necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Dev: Make getInsertID() more consistent with save() · Issue #9233 · codeigniter4/CodeIgniter4