Skip to content
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

feat: add Model field casting #8243

Merged
merged 35 commits into from
Feb 17, 2024
Merged

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Nov 23, 2023

Needs #8230, #8260, #8539

Description
Entity is optional, and some devs do not want to use it.
See https://forum.codeigniter.com/showthread.php?tid=85956
This PR provides field type casting in Model so that all custom entities (or arrays) can use type casting.
This PR can solve #7177, #5905.

Model Field Casting

This casting only works when data comes from and goes to database.

[Object] --- toDataSource() ---> [DB driver]
[Object] <-- fromDataSource() -- [DB driver]

To use this feature, add property $casts to a Model.

    protected $casts = [
        'id'         => 'int',
        'email'      => 'json-array',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

Entity and this feature

Entity casting works at (1)(4), but this casting works at (2)(3).

[App Code] --- (1) --> [Entity] --- (2) --> [Database]
[App Code] <-- (4) --- [Entity] <-- (3) --- [Database]

If you use this feature with Entity, do not use casting in Entity.
Using both casting at the same time does not work.

When using casting in Model, Entity has correct typed PHP values in the attributes.
This behavior is completely different from the previous behavior.
Do not expect the attributes hold raw data from database.

TODO

  • find*()
  • insert()
  • update()
  • save()
  • don't break $useTimestamps
  • don't break Entity

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

@kenjis kenjis added enhancement PRs that improve existing functionalities 4.5 labels Nov 23, 2023
@kenjis kenjis marked this pull request as draft November 23, 2023 06:08
@kenjis kenjis force-pushed the feat-model-type-casting branch 17 times, most recently from 922e64b to 69606e2 Compare November 29, 2023 05:18
@kenjis kenjis force-pushed the feat-model-type-casting branch 6 times, most recently from f958080 to 6183c84 Compare December 2, 2023 08:19
@kenjis
Copy link
Member Author

kenjis commented Dec 2, 2023

Implementation of this feature is complete.
I believe you can use casts in almost any class (custom entity) that is not an Entity.

@kenjis kenjis added database Issues or pull requests that affect the database layer bug Verified issues on the current code behavior or pull requests that will fix them labels Dec 4, 2023
@kenjis kenjis marked this pull request as ready for review February 12, 2024 23:54
@kenjis
Copy link
Member Author

kenjis commented Feb 13, 2024

Updated to use $db->dateFormat and $updateOnlyChanged.

@kenjis kenjis merged commit eac87d1 into codeigniter4:4.5 Feb 17, 2024
47 checks passed
@kenjis kenjis deleted the feat-model-type-casting branch February 17, 2024 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.5 database Issues or pull requests that affect the database layer enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants