Welcome Guest, Not a member yet? Register   Sign In
Set primary key when model's $useAutoIncrecement = false;
#1

Dear all,
I have a very simple table with two columns:
id, varchar(5), primary key, unique
name, varchar(45)
My model extends CodeIgniter's model and I have set $useAutoIncrecement = false;  $useAutoIncrecement = false;$useAutoIncrecement = false;
In the documentation I read:

Quote:If you set $useAutoIncrement to false, then make sure to set your primary key in the database to unique. This way you will make sure that all of Model’s features will still work the same as before.


However I cannot find documentation on then how to set the Primary Key when inserting a new record. The following does not work:
PHP Code:
$data = ['id' => 'UG'name => 'Uganda'];
$myModel->insert($data); 



It will insert a new record but id will be 0.
The only solution I have found to put the primary key in the $allowedFields property, however from the docs:

Quote:The $primaryKey field should never be an allowed field.

Is there any solution I am missing, other than allowing the id field or writing custom queries?

Greetings,
Reply
#2

(This post was last modified: 11-05-2022, 12:23 AM by InsiteFX.)

To be safe with databases we always have a Primary Key:
Code:
id int(11) UNSIGNED NOT NULL AUTOINCREMENT

Change the name of your Key to something that it reflects.
Then just add your fields to the table, your key would be just a UNIQUE KEY
In CodeIgniter set the Primary Key to id.
You should always have a Primary Key like below.

Code:
id int(11) UNSIGNED NOT NULL AUTOINCREMENT

dd
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thank you for your reply however it does not answer my question.

My table has a unique primary key, but there is no way to set that primary key if it is not an auto incteasement.

I need my primary key to a string, not ints.

Codeigniter says I am responsible to provide the correct and unique value for the key but i cant set it using the model.
Reply
#4

my job code:


PHP Code:
class UserGroupModel extends Model {
    protected $table 'user_group';
    protected $primaryKey 'role';
    protected $useAutoIncrement false;
    protected $allowedFields = [
        'role',
        'title',
    ];
    protected $returnType = \App\Models\Entity\UserGroup::class;



Code:
role varchar(20) PRIMARY
title varchar(50)


I think if you ask a question, you can apply a simple option - add a PK to the model. When the time comes, you can fix it. Now there are no serious problems not to change the PK. Database will show other errors
Reply
#5

Your code is exactly my workaround however documenten says PK should never be in allowed fields. Hence my question.
Reply
#6

I think this is a bug.
https://github.com/codeigniter4/CodeIgniter4/pull/6827
Reply
#7

Great it looks like a fix is on the way.
Reply
#8

This bug has been fixed, and it will be included in v4.2.11.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB