fix: toRawArray() should convert DateTime objects to strings#10207
fix: toRawArray() should convert DateTime objects to strings#10207jalexiscv wants to merge 1 commit into
Conversation
|
Hi there, jalexiscv! 👋 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 🤖 |
d486216 to
b7f0796
Compare
neznaika0
left a comment
There was a problem hiding this comment.
Need tests, guide update...
I disagree with the convert. I prefer objects in attributes in my code. Including for the date. Now the attributes will contain strings instead of an object.
Fixes codeigniter4#8302 When a date field is set on an Entity, __set() converts it to a Time object via mutateDate(). However, toRawArray() returned attributes directly without converting Time/DateTime objects back to strings. This fixes toRawArray() to return primitive types by converting DateTimeInterface objects to strings via __toString(). The underlying $this->attributes remain unchanged (still contain Time objects). toArray() behavior is preserved (still returns Time objects). Changes: - Entity::toRawArray(): DateTimeInterface objects converted to strings - Added test verifying: * toRawArray() returns strings for date fields * $this->attributes still contain Time objects * toArray() still returns Time objects (no regression) Ref: codeigniter4#8302
b7f0796 to
67a42dc
Compare
|
Thanks for the review @neznaika0! I've added a unit test (
Full test suite: 139 tests, all passing. Clarification on the conversion: The change only affects the output of So your code that expects objects in attributes continues to work exactly as before. Only the serialized/raw output is affected. |
|
This has not been the case for a long time. The primitives were before version ~4.5. Now attributes can contain any data (using See Original attribute is string, raw data is integer. |
Description
Fixes #8302 - Entity::toRawArray() may return Time object
Problem
When a date field is set on an Entity (e.g.,
$user->updated_at = "2023-12-12 12:12:12"), the__set()method converts it to aTimeobject viamutateDate(). However,toRawArray()was returning$this->attributesdirectly without convertingTime/DateTimeobjects back to strings.Solution
Three changes in
Entity::toRawArray():DateTimeInterfacecheck at the top of the$convertclosure, converting DateTime objects to strings via__toString()array_map($convert, ...)to attributes in the!onlyChangedpath instead of returning$this->attributesdirectly$convert()to non-recursive changed values tootoArray()behavior is unchanged — it still returnsTimeobjects.Changes
One file:
system/Entity/Entity.php!onlyChangedreturn to always usearray_map$convertTesting
toRawArray()returns all string/primitive values ✓toRawArray(true)(onlyChanged) returns no objects ✓toArray()still returnsTimeobjects (no regression) ✓Ref: #8302
Closes #8302