-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Allow named Arguments to be passed to Dto #11575
Conversation
35d8636
to
2a83338
Compare
8be75b6
to
f6ac22b
Compare
This comment was marked as resolved.
This comment was marked as resolved.
86cf2ef
to
091240c
Compare
091240c
to
a983ab2
Compare
4a1cd17
to
c4b9991
Compare
Note that you can only pass scalar expressions or other Data Transfer Objects to the constructor. | ||
|
||
If you use your data-transfer objects for multiple queries but not necessarily with the same parameters, | ||
you can use named arguments or variadic arguments, add the ``named`` keyword in your DQL query before your DTO : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you mentioned named arguments and variadic arguments. Is it necessary to add the NAMED
keyword to use variadic arguments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variadic argument can be used without named
keyword but the array will be "indexed" while with named
it will be associated
17b88d4
to
8334d0f
Compare
8334d0f
to
bccd445
Compare
@derrabus please give this another review. |
waiting for #11619 to push EBNF update |
c5fd47d
to
5388eb8
Compare
.. code-block:: php | ||
|
||
<?php | ||
$query = $em->createQuery('SELECT NEW NAMED CustomerDTO(name: c.name, value: CONCAT(a.city, ' ' , a.zip)) FROM Customer c JOIN c.address a'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if we have only this syntax available CustomerDTO(name: c.name, value: CONCAT(a.city, ' ' , a.zip))
and not the two above that implicitly map the name or the AS naming.
This would in my opinion also make the NAMED
keyword obsolete, which I would like to get rid of as well.
The goal should be that named arguments syntax looks exactly the same as in PHP itself, because the NEW className
somewhat implies that this is what DQL supports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disable implicity map means that we have to write all the field's names for our queries, instead of add a keyword "named".
Remove AS keyword means for those who actually have a named array as result (with AS keyword) to rewrite all the names while in my case just have to add NEW NAMED myDTO(...) and it's done.
Forcing to write: name: c.name, lastname: c.lastname, age: c.age, birthdate: c.birthdate, ... is in my opinion a waste of time for the few fields to rename.
For those as me who have helpers to select fields to add in queries, remove NAMED keyword means to be obliged to name ALL the fields.
As I said in descritpion, final goal is to be able to write c.* and have all the fields by the names,
That's why the first version of this PR have only AS keyword and not the 'PHP syntax'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eltharin I discussed with @greg0ire and @derrabus at our Doctrine core team meetup in person, and I guess the main thing is that we want to avoid adding 2 syntaxes (implicit and AS naming, plus name double colon). We considered both and your arguments and then we would rather remove the PHP-style named arguments syntax and only keep the examples 1+2, so the following should be allowed:
- SELECT NEW NAMED CustomerDTO(a.city, c.name) FROM Customer c JOIN c.address a')
- SELECT NEW NAMED CustomerDTO(c.name, CONCAT(a.city, ' ' , a.zip) AS value) FROM Customer c JOIN c.address a
and this syntax should be removed again:
- SELECT NEW NAMED CustomerDTO(name: c.name, value: CONCAT(a.city, ' ' , a.zip)) FROM Customer c JOIN c.address a
Everything else is 👍 from me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you to all three of you (and other mainteners) for being so open-minded and for your work.
5388eb8
to
e2d8099
Compare
Allow to change argument order or use variadic argument in dto constructor using new named keyword
e2d8099
to
c223b8f
Compare
Thanks @eltharin ! |
@eltharin 💯 thanks |
Allow to change argument order or use variadic argument in dto constructor.
When Dto implements (empty) interface WithNamedArguments, ObjectHydrator will pass an associated key<>value argument to Dto constructor.
Dto can use named arguments or a variadic argument to put values in their properties.
Final Goal (to do after) is to allow
select NEW ItemDto(item.*) FROM App\Entity\Item as item
updated with with the nested New operator PR.