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

DQL NEW Operator to support PHP 8.x named arguments. #9182

Closed
acirulis opened this issue Nov 9, 2021 · 3 comments
Closed

DQL NEW Operator to support PHP 8.x named arguments. #9182

acirulis opened this issue Nov 9, 2021 · 3 comments

Comments

@acirulis
Copy link

acirulis commented Nov 9, 2021

Feature Request

Q A
New Feature yes
RFC no
BC Break no

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:

class BookDTO
{
    public function __construct(
        public int $id,
        public string $title,
        public int $publish_year,
    ) { }
}

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
Copy link
Author

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():

        $queryBuilder
            ->select('p', 'c')
            ->from(Person::class, 'p')
            ->leftJoin('p.contacts', 'c');

        return array_map(
            function (array $result) {
                return CustomerDto::createFromArray($result);
            },
            $queryBuilder->getQuery()->getArrayResult()
        );

@kimhemsoe
Copy link
Member

"Fixed" by #11575 as it was decided not to go with php8 styled named arguments?

@greg0ire
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants