mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
NEW Operator for DTO construction supporting entities aswell as scalar expressions #6910
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @AndreRudolph on GitHub (Jan 19, 2022).
Let's assume I have a scenario where I have a collection and a link entity. A Collection can have multiple links assigned to.
Collection class (minimized example):
and Link class (minimized example):
Let's say I want to retrieve a list of collections with a count of assigned links for each collection. Since I don't want to have an array and instead just one object per row I checked the doctrines documentation and it is possible to create objects from classes that don't need to be an entity itself:
According to https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/dql-doctrine-query-language.html#new-operator-syntax it is only possible to pass scalar expressions to a NEW constructor in DQL. I am wondering what the reason was to only allow scalar expressions and not an entity itself?
If I want to have all the informations of the collection entity (name, description, etc.) would have to create possibly a very large and unmaintainable constructor and query string by listing every single column (property) field of c. Wouldn't it be nice to just pass "c" as the collection entity directly into the CollectionLinkCountViewModel constructor? Like this: