Welcome Guest, Not a member yet? Register   Sign In
model save() function not working?
#1

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
firstname varchar(15)
lastname ...
...

Then, I have the model defined like this:
Code:
<?php namespace App\Models;

use CodeIgniter\Model;

class UsersdataModel extends Model {
    protected $table = 'usersdata';
    protected $primaryKey = 'id';
    protected $useAutoIncrement = false;
    [...]
    protected $allowedFields = [
        'id', 'firstname', 'lastname', ...
    ];
    [...]
}

And finally, in the controller I try:

Code:
...
$usersdataModel = new \App\Models\UsersdataModel();
$usersdataModel->save([
    'id' => user_id(),
    'lastname' => $this->request->getPost('lastname'),
    'firstname' => $this->request->getPost('firstname'),
    ...
]);

This works for the first time (e.g. it inserts) but not for the updates, returning this error:

Code:
mysqli_sql_exception #1062
Duplicate entry '5' for key 'PRIMARY
(user id is 5, indeed).
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!
Reply
#2

(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)
CMS CI4     I use Arch Linux by the way 

Reply
#3

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.
Reply
#4

You have set
PHP Code:
protected $primaryKey 'id'
Therefore, id can not be duplicated.
Here user_id() send a value of 5 to the database and gives an error because it already exists(id=5) in the database.
Reply
#5

remove this 'id' => user_id(),
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#6

(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...
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.

  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
CMS CI4     I use Arch Linux by the way 

Reply




Theme © iAndrew 2016 - Forum software by © MyBB