Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$primaryKey forcefully 'needs' to be auto_increment #2133

Closed
fefo-p opened this issue Aug 12, 2019 · 4 comments
Closed

$primaryKey forcefully 'needs' to be auto_increment #2133

fefo-p opened this issue Aug 12, 2019 · 4 comments

Comments

@fefo-p
Copy link

fefo-p commented Aug 12, 2019

Describe the bug
The primary key of my table is a combination of numbers and letters.
Using or not validation, a new record will not be saved to DB using Entities' save()

CodeIgniter 4 version
CodeIgniter Version: | 4.0.0-beta.4

Affected module(s)
Couldn't figure it out.

Expected behavior, and steps to reproduce if appropriate
A new record should be saved provided I include the primary key value.
Validation rules take care of it being a unique value.
$numero's format is ##-#####/## (12-34567/89)

https://pastebin.com/embed_js/Dz6JFnmk

Context

  • OS: Ubuntu 18.04.2 LTS
  • Web server Apache/2.4.29 (Ubuntu)
  • PHP version 7.2.19
@tada5hi
Copy link
Contributor

tada5hi commented Aug 12, 2019 via email

@fefo-p
Copy link
Author

fefo-p commented Aug 12, 2019

But, I do have a primaryKey to check if record exists or not. So save() method should be able to figure it out.
I don't see the need for it to be auto incrementing. Maybe just to return the "new id" once inserted into the DB. But that has another workaround

@MGatner
Copy link
Member

MGatner commented Aug 13, 2019

That's the nature of save() - if it detects a primary key it assumes an update, otherwise it inserts. See https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Model.php#L529

If you need to force an insert (e.g. when your primary key doesn't exist) use insert(). I think any other approach would require an additional database call which would cripple the convenience of the save() shorthand.

@lonnieezell
Copy link
Member

Sorry, that's not how the convenience function of save is meant to work, as @MGatner pointed out. Using insert and update separately will work fine for you, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants