Skip to content

Conversation

@neznaika0
Copy link
Contributor

Description
Fixes #9711
It is better to merge the develop branch for the review.

  • Removed the check for a valid ID as method. It's too hard to choose a direction. Returned the deleted checks
  • Added dots in comments, some formatting
  • Updated PHPDoc in phpstan style
  • Inherited comments have been removed
  • Updated phpstan-baseline

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

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update Deptrac.

Comment on lines 976 to 977
// if (is_numeric($id) || is_string($id)) {
if (! in_array($id, [null, 0, '0'], true) && (is_numeric($id) || is_string($id))) {
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@neznaika0 neznaika0 Dec 11, 2025

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

Copy link
Member

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.

Copy link
Contributor Author

@neznaika0 neznaika0 Dec 11, 2025

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()

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

@michalsn michalsn added refactor Pull requests that refactor code 4.7 labels Dec 11, 2025
@neznaika0 neznaika0 force-pushed the refactor/model-types branch from de4b6e8 to 9b5e64b Compare December 11, 2025 20:03
Copy link
Member

@michalsn michalsn left a 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.

@michalsn michalsn merged commit c9d3dcd into codeigniter4:4.7 Dec 13, 2025
50 checks passed
@michalsn
Copy link
Member

Thank you @neznaika0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.7 refactor Pull requests that refactor code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants