CodeIgniter Forums
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
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!


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


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