Skip to content

Conversation

patel-vansh
Copy link
Contributor

Description
This PR fixes the bug where casting was not performed on the rows during insertBatch() and updateBatch() methods inside BaseModel file.

Fixes #9695.

Checklist:

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

Copy link

mergeable bot commented Sep 1, 2025

Hi there, patel-vansh! 👋

Thank you for sending this PR!

We expect the following in all Pull Requests (PRs).

Important

We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works.

If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work
on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Sincerely, the mergeable bot 🤖

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.

To fix code style problems, simply run: composer cs-fix.

After making changes as suggested in my comments, you can run checks: composer sa, and see if there are still any problems. If you don't know how to fix them, please push the changes, and we will try to investigate.

@patel-vansh
Copy link
Contributor Author

Hello @michalsn, I see there are two checks failing. I don't know how to fix them, can you please guide me?

I think the copy-paste detection check can be solved by creating a new function (if its okay to introduce a new function).

For the other check, I really can't see where its going wrong.

Thanks for you help.

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.

Add above comments to the tests, and then run: composer phpstan:baseline - this will update the baseline. Not sure why this happened, but nothing is added, just the line numbers are changed, so we should be good.

As for CPD handling, we can try to make a separate method, but it probably will be catched again, because the code will be very similart to one used in transformDataToArray() method. Anyway, we should try to create something like transformDataRowToArray(). It should comment in PHPDocs like:

/**
 * @used-by insertBatch()
 * @used-by updateBatch()
 *
 * @deprecated Since 4.6.4, temporary solution - will be removed in 4.7
 */

If it still be catched by CPD, you can try an "ugly hack" by changing code comments a bit or entirely removing them from this method.

@patel-vansh
Copy link
Contributor Author

I've created a protected performCasting function which does casting.

@michalsn
Copy link
Member

michalsn commented Sep 2, 2025

Instead of using performCasting(), please try the approach below. Abstracting only a small portion of the transformation feels unnecessary, especially for a temporary method.

protected function transformDataRowToArray(array $row): array
{
    if ($this->useCasts()) {
        if (is_array($row)) {
            $row = $this->converter->toDataSource($row);
        } elseif ($row instanceof stdClass) {
            $row = (array) $row;
            $row = $this->converter->toDataSource($row);
        } elseif ($row instanceof Entity) {
            $row = $this->converter->extract($row);
        } elseif (is_object($row)) {
            $row = $this->converter->extract($row);
        }
    } elseif (is_object($row) && ! $row instanceof stdClass) {
        $row = $this->objectToArray($row, false, true);
    }

    if (is_object($row)) {
        $row = (array) $row;
    }

    return $this->timeToString($row);
}

@patel-vansh
Copy link
Contributor Author

I've made requested changes. Also, retained comments.

@patel-vansh patel-vansh requested a review from michalsn September 2, 2025 14:02
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.

Looks good, thanks! Let's see what others think.

For reference: transformDataRowToArray() method is deprecated. After merging, I'll create a new PR targeting the 4.7 branch, where we'll use only transformDataToArray(). That method depends on additional settings such as allowEmptyInserts and updateOnlyChanged. Applying those requirements now could cause problems in some use cases, so introducing them in the 4.7 branch is the safer approach.

If someone else merges this, please use "Squash and merge".

@michalsn michalsn added the bug Verified issues on the current code behavior or pull requests that will fix them label Sep 4, 2025
@michalsn michalsn merged commit ea9612f into codeigniter4:develop Sep 4, 2025
50 checks passed
@michalsn
Copy link
Member

michalsn commented Sep 4, 2025

Thank you @patel-vansh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Casting happens in insert but not in insertBatch
3 participants