model save() function not working? - 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: model save() function not working? (/showthread.php?tid=81583) |
model save() function not working? - csebe - 03-21-2022 Hi all, CodeIgniter 4.1.9 installation. I try to use the save() method (described here: https://codeigniter.com/user_guide/models/model.html) but it seems it is not working?? I have created the "usersdata" mysql table like this: Code: id int(11) primary key Then, I have the model defined like this: Code: <?php namespace App\Models; And finally, in the controller I try: Code: ... This works for the first time (e.g. it inserts) but not for the updates, returning this error: Code: mysqli_sql_exception #1062 If I remove the primary key constraint from the id field in the table, it keeps inserting rows with the same id (5). Am I missing something or it is a bug? Please note I am aware that I can do select then insert() or update(), but the point is to understand why save() is not working. Thank you! RE: model save() function not working? - captain-sensible - 03-21-2022 i only read as far as the first line field ID if int (11) do you realize that means 11 digits of type INT that you are setting id to over a billion entries ? varchar best if thats varchar(255) RE: model save() function not working? - csebe - 03-21-2022 This has nothing to do with the question... Maybe you want to search a bit about "defaults in mysql for an int field" before polluting the thread... Anyways, it seems that the problem is solved now. For the posterity: I have mistakenly set the "deleted_at" field as default='now()' in mysql, and together with Code: protected $useSoftDeletes = true; in the model definition, and strange things happened (like not even the find() did work because the row was actually marked as deleted). Thanks. RE: model save() function not working? - datamweb - 03-21-2022 You have set PHP Code: protected $primaryKey = 'id'; Here user_id() send a value of 5 to the database and gives an error because it already exists(id=5) in the database. RE: model save() function not working? - luckmoshy - 03-21-2022 remove this 'id' => user_id(), RE: model save() function not working? - captain-sensible - 03-22-2022 (03-21-2022, 05:18 AM)csebe Wrote: This has nothing to do with the question... Maybe you want to search a bit about "defaults in mysql for an int field" before polluting the thread... Possibly not but this is not one of those sites where nerdy little oinks proliferate or where i will put up with being spoken to rudely - i'd rather leave the site |