fix: toRawArray() should convert DateTime objects to strings #10207
Hi there, jalexiscv! :wave:
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 🤖
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.
Thanks for the review @neznaika0!
I've added a unit test (testToRawArrayConvertsDateTimeToString) that verifies:
1. toRawArray() returns strings for date fields ✓
2. $this->attributes still contain Time objects (unchanged) ✓
3. toArray() still returns Time objects (no regression) ✓
Full test suite: 139 tests, all passing.
Clarification on the conversion: The change only affects the output of toRawArray() — $this->attributes are never modified. They still contain Time objects. The toRawArray() method is designed to return raw/primitive values (as its name implies), while toArray() keeps returning Time objects.
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 DataCaster in the entity or DataConverter for models). For understanding, after selecting the Model::find(), an entity can convert a date string into an object. Therefore, toRowArray() will contain more than just primitives. Look at the tests.
See https://github.com/codeigniter4/CodeIgniter4/blob/2a1c348ce5194e648918d77d9c808951d0d2d4d4/tests/system/Entity/EntityTest.php#L384
Original attribute is string, raw data is integer.
neznaika0
jalexiscv
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():1. Add
DateTimeInterfacecheck at the top of the$convertclosure, converting DateTime objects to strings via__toString()2. Always apply
array_map($convert, ...)to attributes in the!onlyChangedpath instead of returning$this->attributesdirectly3. Apply
$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: https://github.com/codeigniter4/CodeIgniter4/issues/8302
Closes #8302