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

Entity rework #7995

Closed
wants to merge 19 commits into from
Closed

Entity rework #7995

wants to merge 19 commits into from

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Sep 30, 2023

Description
Fixes #5905
Supersedes #6284

  • Now all Entity::$attributes have (cast) PHP values, not raw values from database
  • add CastInterface::toDatabase() and CastInterface::fromDatabase()
  • add Entity::toDatabase() to return array for database and Entity::fromDatabase() to rebuild Entity from database data
[App Code] --- set() --> [Entity] --- toDatabase() ---> [Database]
[App Code] <-- get() --- [Entity] <-- fromDatabase() -- [Database]

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 bug Verified issues on the current code behavior or pull requests that will fix them breaking change Pull requests that may break existing functionalities 4.5 labels Sep 30, 2023
@kenjis kenjis marked this pull request as draft September 30, 2023 01:40
@kenjis kenjis force-pushed the entity-rework branch 2 times, most recently from a37b9d5 to 9995e42 Compare September 30, 2023 02:17
@kenjis kenjis force-pushed the entity-rework branch 2 times, most recently from c5576d9 to 90f84ae Compare September 30, 2023 05:40
$entity->fill(['username' => 'johnsmith', 'active' => false, 'memo' => ['foo', 'bar']]);
$model->save($entity);

$user = $model->asObject(get_class($entity))->find(1);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik Probably get_class($entity) causes the Rector error:

 [ERROR] Could not process "tests/system/Entity/EntityLiveTest.php" file, due to:                                       
         "assert(count($exprType) === 1)". On line: 170  

Do you know something?

@kenjis kenjis force-pushed the entity-rework branch 2 times, most recently from a349acd to 404e749 Compare October 3, 2023 23:36
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear this is leaning into the issue from last time. Most of the handling is correct but I would argue it is in the wrong place. We keep tying our data representations to the actual database driver. This does move the responsibility off of Entity (somewhat) but now bakes those assumptions into Cast.

What about NoSQL databases? in-memory mocks? entities bound for API endpoints? Why should they all be bound to MySQL's limitations?

Single responsibility: let Entity hold the data and represent the object, let Cast convert between different data types and act as a gate for bad data, let Model send associative arrays of data back and forth from storage.

Since Model is the only class that actually cares about the infrastructure: it should handle any necessary data conversions. The problem is that BaseModel/Model is already a mess, and we've pushed most of our driver-specific implementation into the Query Builders.

@kenjis
Copy link
Member Author

kenjis commented Oct 8, 2023

Since Model is the only class that actually cares about the infrastructure: it should handle any necessary data conversions.

let Model send associative arrays of data back and forth from storage.

Do you mean Model should have logic for toDatabase() and fromDatabase()?

CI's QueryBuilder has the feature to use Custom Result Objects:
https://codeigniter4.github.io/CodeIgniter4/database/results.html#getcustomresultobject
If Model has the data conversion logic, QueryBuilder can't use it when it creates an Entity.
Do you say we should make the feature deprecated?

@MGatner
Copy link
Member

MGatner commented Oct 10, 2023

@kenjis I'm afraid I don't have good answers, only concerns. I believe the current architecture is bad, and the mess of our code between entity-model-builder shows for it. Every time an issue or feature comes up and we commit more to this architecture I get worried we are digging ourselves in deeper. But the reality is that we can't afford a complete refactor right now, nor could we even release it (except maybe as a standalone).

For the issue at hand: if we can find a way to let Model (SQL-specific) handle to/from database I think that's the best place for the new code to reside, architecturally speaking.

Should not pass null to Cash handlers.
But I don't know it makes sense.
 [ERROR] Could not process "tests/system/Entity/EntityLiveTest.php" file, due to:
         "assert(count($exprType) === 1)". On line: 170
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.5 breaking change Pull requests that may break existing functionalities bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants