mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
DQL NEW Operator to support PHP 8.x named arguments. #6872
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 @acirulis on GitHub (Nov 9, 2021).
Feature Request
Summary
Would it be possible for DQL NEW operator (https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/reference/dql-doctrine-query-language.html#new-operator-syntax) to support PHP 8.0 named arguments (https://stitcher.io/blog/php-8-named-arguments) additionally to regular positional ones? As far as I see, DQL NEW() syntax is great for transforming Doctrine results to DTO objects but only drawback is use of positional arguments, which can be easily mixed up in the long-term (human error). Named arguments would fix that.
Consider class:
Current DQL:
$query = $em->createQuery('SELECT NEW BookDTO(b.id, b.name, b.publish_year) FROM Book b');Proposed DQL:
$query = $em->createQuery('SELECT NEW BookDTO(id: b.id, name: b.name, publish_year: b.publish_year) FROM Book b');@acirulis commented on GitHub (Nov 30, 2021):
Short update: I`ve found since that DQL NEW keyword has limitations ("Note that you can only pass scalar expressions to the constructor.") and figured better following approach for instantiating DTOs with array_map():
@kimhemsoe commented on GitHub (Nov 13, 2024):
"Fixed" by https://github.com/doctrine/orm/pull/11575 as it was decided not to go with php8 styled named arguments?
@greg0ire commented on GitHub (Nov 13, 2024):
That and also there is an alternate syntax closer to SQL that should address the issue (although the issue was not clearly worded in the first place, only the desired solution).