-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Entity: cast scalar types when setting #5944
Conversation
This way Entity stores its attributes using types as specified in the $casts. This results in: 1. Cleaner logic: data is always stored as expected. Not only read. 2. Fixing reliability of hasChanged() Without this change what seemed like setting already set (identical to read) value could actually change type and result in hasChanged() unexpectedly returning true. Ref: codeigniter4#5905 Signed-off-by: Rafał Miłecki <[email protected]>
Thank you for sending a PR. We expect all contributions to conform to our style guide, be commented (inside the PHP source files), be documented (in the user guide), and unit tested (in the test folder). This PR is not unit tested, and an existing test failed. Your PRs need to be GPG-signed before they will be considered. |
Aside from the lack of testing, this appears to be a major design change and requires careful consideration. |
Entities have gotten very confusing, but: this is a mistake. The stored entity value is supposed to represent its "database state", so when Model converts an entity in On a personal note, I've found |
Follow-up example to my personal note: https://github.com/tattersoftware/codeigniter4-firebase/blob/develop/src/Firestore/Collection.php#L188 Notice how for my Firebase (NoSQL) driver is explicitly use |
Closing this as stale. @rmilecki if you need entity attributes stored as casts I recommend making your own library. If you really think this belongs somewhere in the framework then start a forum thread to gauge interest, then it could be added as a new feature (instead of changing the current behavior). |
Thanks for your comments. If I can share my opinion, the whole casting in
that is inconsistent and results in hard to understand behaviour. Maybe there are good reasons for that I simply can't understand (I'm not advanced programmer). Please just note that end users can get confused by that. Also it seems to break |
I'm sorry for the confusion. I would like to help you work through this so we can get your suggestions on how this could have been more clear. I think you still aren't understanding how Entity casting works, as this is (mostly) untrue:
The idea is that the Entity class gives the developer developer-friendly types and the database database-friendly types. With a few exceptions, Entity does not cast user-set values (this was what your PR was adding). From the User Guide:
https://codeigniter4.github.io/CodeIgniter4/models/entities.html#property-casting If there's some confusion it probably comes from some of the Cast names. For example CSV casting is "database: csv_string, developer: array", whereas scalar casts are names for the developer types (Boolean Cast is "developer: boolean"). I'm not sure what can be done to improve that but I think the User Guide describes each cast type pretty well. |
Thanks @MGatner, I just read everything carefully again and I think I understand it now. AFAIU: casting is meant to affect reads only making accessing (reading) data easier. Now I think about it it sounds sane and useful. It should be enough for most (or all) cases. It does what it's supposed to do. My real problem is unreliable |
I think a radical solution requires a redesigned new Entity. But that would be a major undertaking and would destroy compatibility. |
@kenjis: we could introduce another class though, right? |
@rmilecki Yes. |
I'm honestly not fond of having multiple entity classes personally, as it just makes things more confusing when trying to decide which to use. I think we'll need to brainstorm on a good solution for this. |
My opinion... the current Entity is trying to do too many things. It is pretty useful as a "Jack of all trades" but starts to fail when you need depth or complexity. Working with NoSQL it is almost useless, since data types are much more consistent between database and code. Trying to use them as dynamic objects not connected to databases sometimes runs into weird behavior. I mostly agree with Lonnie that we don't need seven different Entities in the framework - mostly those should be an extension class in personal projects or libraries - but I do think we could get creative for version 5. |
Description
This way Entity stores its attributes using types as specified in the
$casts. This results in:
Without this change what seemed like setting already set (identical to
read) value could actually change type and result in hasChanged()
unexpectedly returning true.
Ref: #5905
Signed-off-by: Rafał Miłecki [email protected]