![]() |
Behaviour of getInsertID() when using save() - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Behaviour of getInsertID() when using save() (/showthread.php?tid=91800) |
Behaviour of getInsertID() when using save() - evansharp - 10-14-2024 When using the CI Model for basic CRUD, does `getInsertID()` return null when a preceding `save()` did an UPDATE rather than INSERT? Or does it return the affected primary key regardless? This detail would be a great addition to that method's documentation entry. RE: Behaviour of getInsertID() when using save() - InsiteFX - 10-14-2024 getInsertId() is in system/BaseModel.php This is all that I can find on it. PHP Code: /** RE: Behaviour of getInsertID() when using save() - evansharp - 10-20-2024 (10-14-2024, 11:33 PM)InsiteFX Wrote: getInsertId() is in system/BaseModel.phpThat function is just a getter for the BaseModel property. The property is set when Model::doInsert() actually performs a query: PHP Code: // If insertion succeeded then save the insert ID Unfortunately, Model::update() does not set this property: PHP Code: public function update($id = null, $row = null): bool And if we look at Model: ![]() PHP Code: public function save($row): bool I guess the answer to my question is that the behaviour is inconsistent. The $this->insertID is only set when save() does an insert. I suppose it makes sense when thinking about the update() and insert() functions being called explicitly (if updating, one knows the ID already), but the point of the helper is to be able to shove a record into it without having to determine if it's new or not. If I have to write the logic to determine if an ID exists, a pretty common need for performing further inserts that use it as a FK, it defeats the purpose of save(). How opposed would we be to having update() save the affected ID so that save() works better? I can PR if there is no reason not to... RE: Behaviour of getInsertID() when using save() - InsiteFX - 10-20-2024 I have to agree with you on the inconsistent nature of it. A framework should be consistent through out. I would go to GitHub and put in an issue on it and see if they would like to take your PR. |