Skip to content

Commit

Permalink
update doc with named keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
eltharin committed Aug 27, 2024
1 parent 62f0a5c commit 86cf2ef
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions docs/en/reference/dql-doctrine-query-language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -609,23 +609,21 @@ You can also nest several DTO :
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 ``WithNamedArguments`` to your Dto :
you can use named arguments or variadic arguments, add ``named`` keyword in your DQL querie before your Dto :

.. code-block:: php
<?php
use Doctrine\ORM\WithNamedArguments;
class CustomerDTO implements WithNamedArguments
class CustomerDTO
{
public function __construct(
public string|null $name = null,
public string|null $email = null,
public string|null $city = null,
public mixed|null $value = null,
)
{
public AddressDTO|null $address = null,
) {
}
}
Expand All @@ -634,7 +632,7 @@ And then you can select the fields you want in the order you want, and the ORM w
.. code-block:: php
<?php
$query = $em->createQuery('SELECT NEW CustomerDTO(a.city, c.name) FROM Customer c JOIN c.address a');
$query = $em->createQuery('SELECT NEW NAMED CustomerDTO(a.city, c.name) FROM Customer c JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
// CustomerDTO => {name : 'SMITH', email: null, city: 'London', value: null}
Expand All @@ -644,7 +642,7 @@ ORM will also look column aliases before columns names :
.. code-block:: php
<?php
$query = $em->createQuery('SELECT NEW CustomerDTO(c.name, CONCAT(a.city, ' ' , a.zip) AS value) FROM Customer c JOIN c.address a');
$query = $em->createQuery('SELECT NEW NAMED CustomerDTO(c.name, CONCAT(a.city, ' ' , a.zip) AS value) FROM Customer c JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
// CustomerDTO => {name : 'DOE', email: null, city: null, value: 'New York 10011'}
Expand All @@ -654,7 +652,17 @@ For aliases a field you can use SQL notation with `AS` keyword, or the PHP named
.. code-block:: php
<?php
$query = $em->createQuery('SELECT NEW CustomerDTO(name: c.name, value: CONCAT(a.city, ' ' , a.zip)) FROM Customer c JOIN c.address a');
$query = $em->createQuery('SELECT NEW NAMED CustomerDTO(name: c.name, value: CONCAT(a.city, ' ' , a.zip)) FROM Customer c JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
// CustomerDTO => {name : 'DOE', email: null, city: null, value: 'New York 10011'}
The `NAMED` keywork must be added to all dto you want to instantiate :

.. code-block:: php
<?php
$query = $em->createQuery('SELECT NEW NAMED CustomerDTO(name: c.name, address: NEW NAMED AddressDTO(a.street, a.city, a.zip)) FROM Customer c JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
// CustomerDTO => {name : 'DOE', email: null, city: null, value: 'New York 10011'}
Expand Down

0 comments on commit 86cf2ef

Please sign in to comment.