Type: New Feature
Affects Version/s: None
Fix Version/s: None
Security Level: All
I created a discussion in the mailing list (https://groups.google.com/forum/#!topic/doctrine-dev/7HfEqOwhkDk) but no answer, so I'm moving the discussion here.
The Criteria API provides an abstraction to filter collections/repositories, may they be in memory (filtering in PHP) or in a database (filtering using a SQL query).
I was thinking about an "Expression" API which would work like array_map to apply changes in bulk to entities.
For example, if you have a collection where entities have a $position field, if I insert an item in the list, I have to increment the position of all the following items. For this, I have 2 options:
- PHP: loading the entities in memory, iterate over them and increment $position
- SQL: run a UPDATE position=position + 1 WHERE …
The second option is much more efficient, but it breaks the abstraction of the model because I have some behavior written explicitely in SQL. Furthermore, the objects loaded in memory will note be updated with the SQL query.
So the "Expression" API would work like the Criteria API:
The expression here would be applied:
- in memory if the collection is already loaded
- else in database using SQL
The expression could be applied to a Collection and to a Repository (like the Criteria).
About the API offered by the Expression class, there are several options:
I think I like the third one better because we have full control over the supported operations, and adding support to new kinds of operation is easy. The first one would require defining a whole language of allowed expressions...
What do you think of all that?