-
Notifications
You must be signed in to change notification settings - Fork 2k
refactor: Types for BaseModel, Model and dependencies
#9830
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
Conversation
michalsn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update Deptrac.
system/BaseModel.php
Outdated
| // if (is_numeric($id) || is_string($id)) { | ||
| if (! in_array($id, [null, 0, '0'], true) && (is_numeric($id) || is_string($id))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of this check is to convert a variable to an array so that the events can properly consume it. Please revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I adapted the old PR, lost contact with the Model. in doDelete() doUpdate() we have already checked this. That's right.
I wanted the array to be passed to the event only when it is a valid value...
$eventData = [
'id' => $id, // [0 => '0'] It looks wrong
'purge' => $purge,
];I'll cancel it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, after deleting the check, we get errors in the tests. I was confused by the comment - this change was already in 4.7
To improve, you can add an array check for empty values [0 => 123, 1 => 456, 2 => null].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, there is no such code in either the develop or 4.7 branches. The diff is clear - this change was introduced in your commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, I'm looking at delete()
CodeIgniter4/system/BaseModel.php
Lines 1101 to 1109 in 4c88a27
| public function delete($id = null, bool $purge = false) | |
| { | |
| if (is_bool($id)) { | |
| throw new InvalidArgumentException('delete(): argument #1 ($id) should not be boolean.'); | |
| } | |
| if (! in_array($id, [null, 0, '0'], true) && (is_numeric($id) || is_string($id))) { | |
| $id = [$id]; | |
| } |
BUT update() requires a similar behavior, otherwise, we will get a bad SQL. The principle is the same - you cannot update or delete the values of ID 0, null,.. Am I wrong?
UPDATE `db_job` SET `name` = 'Foo Bar'
WHERE `db_job`.`id` IN (0)
UPDATE `db_job` SET `name` = 'Foo Bar'
WHERE `db_job`.`id` IN ('')
To confirm, I can update the tests for provideUpdateThrowDatabaseExceptionWithoutWhereClause
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you're talking about. Events always need an array. This can be fixed by moving the check to doUpdate() doDelete().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalsn @paulbalandan I have returned the previous check state to the BaseModel.
Add a method to check the isValidId($id) (Yes, I found a use for it).
Added tests for empty values for delete($id)/update($id). Is fine - we get the "Empty WHERE" exception.
But we can specify where('id', [15 => 123, 16 => null])->delete() In this case, 100% of the $id will be empty and sql does not need to be executed, but this is not an error.
Question: Do I need to check binds for an empty PK? We get SQL error '... near ")"' in "... WHERE id IN ()".
Next question is whether it is necessary to handle the case with where('id", ...)->delete('0')?
These changes should improve protection against the error of missing an ID when updating a record. Or should the user just get an SQL exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not make any changes to the behavior of checking the PK in this PR. It has to be addressed in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it in new PR. I want to understand whether to create it or leave the sql exception as it is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but we won't be discussing a "virtual PR" here. I'm not sure what changes you've made there, and I needed some time to review the current state so we can choose the right direction moving forward.
de4b6e8 to
9b5e64b
Compare
michalsn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks good to me.
|
Thank you @neznaika0 |
Description
Fixes #9711
It is better to merge the develop branch for the review.
I have re-examined the proposed ID check method. I realized that it is less useful for BaseModel. The method is useful in the Model. As long as you consider the check to be breaking changes, let everything stay as it was.
There is a note for
row_array- in the case of casting, the value can be mixed. Contain any type inside, which is converted to scalars. Find the error in utils/phpstan-baseline/argument.type.neon DataConverterModelTest.phpChecklist: