-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: Casting in insertBatch and updateBatch methods. #9698
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
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 See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md Sincerely, the mergeable bot 🤖 |
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.
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.
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. |
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.
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.
I've created a protected |
Instead of using 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);
} |
I've made requested changes. Also, retained comments. |
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.
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".
Thank you @patel-vansh |
Description
This PR fixes the bug where casting was not performed on the rows during
insertBatch()
andupdateBatch()
methods insideBaseModel
file.Fixes #9695.
Checklist: