← Back to Pull requests

fix: toRawArray() should convert DateTime objects to strings #10207

Open
jalexiscv opened this pull request with 46 lines diff across 2 files
View on GitHub
jalexiscv jalexiscv commented on May 18, 2026 02:58 UTC
Author

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 a Time object via mutateDate(). However, toRawArray() was returning $this->attributes directly without converting Time/DateTime objects back to strings.

$user->updated_at = "2023-12-12 12:12:12";
var_dump($user->toRawArray());
// Before fix: "updated_at" => Time object (BUG)
// After fix:  "updated_at" => "2023-12-12 12:12:12" (string)

Solution

Three changes in Entity::toRawArray():

1. Add DateTimeInterface check at the top of the $convert closure, converting DateTime objects to strings via __toString()
2. Always apply array_map($convert, ...) to attributes in the !onlyChanged path instead of returning $this->attributes directly
3. Apply $convert() to non-recursive changed values too

toArray() behavior is unchanged — it still returns Time objects.

Changes

One file: system/Entity/Entity.php

  • Added DateTimeInterface → string conversion in convert closure

  • Changed !onlyChanged return to always use array_map

  • Changed non-recursive changed value assignment to use $convert
  • Testing

  • toRawArray() returns all string/primitive values ✓

  • toRawArray(true) (onlyChanged) returns no objects ✓

  • toArray() still returns Time objects (no regression) ✓
  • Ref: https://github.com/codeigniter4/CodeIgniter4/issues/8302
    Closes #8302

    Diff: 46 lines (+42 / -4) across 2 files Reviewer: neznaika0 Review duration: 212219m
    All Vouch review confidence checks passed
    Residual risk 0.05 = Change Risk (0.34) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.34 · 46 lines diff across 2 files
    0.34
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.92 · 3 substantive comment(s)
    0.92
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    84%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.230
    Real PR Conversation Timeline
    5 items
    mergeable[bot] mergeable[bot] commented on May 18, 2026 02:58 UTC
    Bot

    Hi there, jalexiscv! :wave:

    Thank you for sending this PR!

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

  • PRs must be sent to the appropriate branch

  • All git commits must be GPG-signed

  • Must follow our style guide

  • Be commented in the PHP source file

  • Be documented in the user guide

  • Be unit tested

  • Pass all checks in GitHub Actions
  • [!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 🤖

    neznaika0 neznaika0 commented on May 18, 2026 03:49 UTC
    Reviewer

    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.

    jalexiscv jalexiscv commented on May 18, 2026 03:59 UTC
    Author

    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.

    neznaika0 neznaika0 commented on May 18, 2026 04:20 UTC
    Contributor

    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.

    Reviewers
    Assignees
    jalexiscv jalexiscv
    Labels
    risk: low confidence: 84.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 34.5% (LOW) · Higher = larger blast radius
    34.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 84.5% · Lower = rushed or shallow review
    84.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 5.3% · Safe to merge
    5.3%