Skip to content

fix: Casting in insertBatch and updateBatch methods. - #9698

Merged
michalsn merged 8 commits into
codeigniter4:developfrom
patel-vansh:develop
Sep 4, 2025
Merged

fix: Casting in insertBatch and updateBatch methods.#9698
michalsn merged 8 commits into
codeigniter4:developfrom
patel-vansh:develop

Conversation

@patel-vansh

Copy link
Copy Markdown
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

@mergeable

mergeableBot commented Sep 1, 2025

Copy link
Copy Markdown

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 🤖

@michalsnmichalsn left a comment

Copy link
Copy Markdown
Member

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.

Comment threadsystem/BaseModel.php Outdated
Comment threadsystem/BaseModel.php Outdated
Comment threadtests/system/Models/InsertModelTest.php
Comment threadtests/system/Models/InsertModelTest.php Outdated
Comment threadtests/system/Models/UpdateModelTest.php
@patel-vansh

Copy link
Copy Markdown
ContributorAuthor

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.

@michalsnmichalsn left a comment

Copy link
Copy Markdown
Member

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.

Comment threadtests/system/Models/InsertModelTest.php Outdated
Comment threadtests/system/Models/UpdateModelTest.php Outdated
@patel-vansh

Copy link
Copy Markdown
ContributorAuthor

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

@michalsn

Copy link
Copy Markdown
Member

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

protectedfunctiontransformDataRowToArray(array$row): array
{
if ($this->useCasts()) {
if (is_array($row)) {
$row = $this->converter->toDataSource($row);
} elseif ($rowinstanceof stdClass) {
$row = (array) $row;
$row = $this->converter->toDataSource($row);
} elseif ($rowinstanceof Entity) {
$row = $this->converter->extract($row);
} elseif (is_object($row)) {
$row = $this->converter->extract($row);
}
} elseif (is_object($row) && ! $rowinstanceof stdClass) {
$row = $this->objectToArray($row, false, true);
}
if (is_object($row)) {
$row = (array) $row;
}
return$this->timeToString($row);
}

@patel-vansh

Copy link
Copy Markdown
ContributorAuthor

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

@michalsnmichalsn left a comment

Copy link
Copy Markdown
Member

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

@michalsnmichalsn 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:developSep 4, 2025
50 checks passed
@michalsn

Copy link
Copy Markdown
Member

Thank you @patel-vansh

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

Labels

bugVerified 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

@patel-vansh@michalsn@paulbalandan