model save() function not working? |
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!
03-21-2022, 04:50 AM
(This post was last modified: 03-21-2022, 04:52 AM by captain-sensible. Edit Reason: info on varchar )
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)
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.
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.
remove this 'id' => user_id(),
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals
(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 |
Welcome Guest, Not a member yet? Register Sign In |