Issues with $model->save() and duplicate primary key - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Issues with $model->save() and duplicate primary key (/showthread.php?tid=87444) |
Issues with $model->save() and duplicate primary key - joho - 04-20-2023 Quite similar to this thread, but not identical: https://forum.codeigniter.com/showthread.php?tid=81583 I use the $model->save() method to let it do the "magic" of choosing between update/insert. Yet, when the primary key is passed in the data array to save(), I get a duplicate key error. Pseudo code: PHP Code: $save_data = [ 'name' => $person_name, 'city' => $person_city ]; I'm obviously not doing it right, but I thought that $model->save() would see the primary key ('id') being present in the $save_data and thus do an update instead of an insert. What's also strange is that in the validation rules, I specify that $person_name must be unique, and pass it the 'id' key, and the validation goes through OK when editing a record and just modify the $person_city (i.e. it does properly check that the record being edited is the one with the same $person_name, and thus it's not a duplicate). Ah, yes ... Code: class mySpecialModel extends Model makes it work a little bit better :-) |