-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(model): primary key validation #9840
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
base: 4.7
Are you sure you want to change the base?
Conversation
|
Great work! I tried to do this in the first PR, unsuccessfully. I won't be able to test locally, looking at the tests everything should work as planned. Since I am refactoring the code more, I would not like to add new phpstan errors. Maybe you should ignore them completely in the tests? |
I'm not sure what you mean. I added return types to the providers to avoid introducing new errors. |
a706eaa to
262a996
Compare
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
Description
This PR adds primary key validation to the Model's
insert(),insertBatch(),update(), anddelete()methods (forinsert*whenuseAutoIncrementis disabled). This ensures that invalid primary key values are caught early with clear error messages, rather than causing confusing database errors.Previously, when you passed invalid values like 0, '0', or empty strings as primary keys, these values would often be silently ignored by the query builder.
The new
validateID()method inBaseModelnow explicitly rejects these invalid primary key values:null(for insert when auto-increment is disabled)0(integer zero)'0'(string zero)''(empty string)trueandfalse(booleans)[](empty array)[[1, 2]]When these values are detected, the model throws an
InvalidArgumentExceptionwith a specific message like "Invalid primary key: 0 is not allowed".Validation timing
update()anddelete(): Validation happens inBaseModelbeforebeforeUpdate/beforeDeleteevents, preventing invalid values from reaching event handlersinsert()andinsertBatch()(when auto-increment is disabled): Validation happens inModel::doInsert()andModel::doInsertBatch()after thebeforeInsertevent (allowing devs to use this event to produce the PK), but before database queriesIf developers have a legitimate need to use values like
0or'0'as primary keys (e.g., legacy systems), they can override thevalidateID()method in their model to customize the behavior.Checklist: