Skip to content

Commit

Permalink
Update docs/en/reference/dql-doctrine-query-language.rst
Browse files Browse the repository at this point in the history
changes after review
  • Loading branch information
eltharin committed Aug 23, 2024
1 parent 2a83338 commit ce472bb
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 34 deletions.
17 changes: 13 additions & 4 deletions docs/en/reference/dql-doctrine-query-language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ 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 query but not ever the same parameters, you can use named arguments or variadic arguments, add ``WithNamedArguments`` to your Dto :
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 :

.. code-block:: php
Expand All @@ -618,27 +619,35 @@ If you use your data-transfer objects for multiple query but not ever the same p
class CustomerDTO implements WithNamedArguments
{
public function __construct(public string|null $name = null, public string|null $email = null, public string|null $city = null, public mixed|null $value = null)
public function __construct(
public string|null $name = null,
public string|null $email = null,
public string|null $city = null,
public mixed|null $value = null,
)
{
}
}
And then you can select the fields you want in order you want :
And then you can select the fields you want in the order you want :

.. code-block:: php
<?php
$query = $em->createQuery('SELECT NEW CustomerDTO(a.city, c.name) FROM Customer c JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
you can either aliases column :
// CustomerDTO => {name : 'SMITH', email: null, city: 'London', value: null}
you can also aliases column :

.. 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');
$users = $query->getResult(); // array of CustomerDTO
// CustomerDTO => {name : 'DOE', email: null, city: null, value: 'New York 10011'}
Using INDEX BY
~~~~~~~~~~~~~~
Expand Down
5 changes: 0 additions & 5 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,4 @@
<!-- https://github.com/doctrine/orm/issues/8537 -->
<exclude-pattern>src/QueryBuilder.php</exclude-pattern>
</rule>

<rule ref="Generic.Files.LineEndings.InvalidEOLChar">
<exclude name="Generic.Files.LineEndings.InvalidEOLChar"/>
</rule>

</ruleset>
12 changes: 3 additions & 9 deletions src/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ abstract protected function hydrateAllData(): mixed;
* newObjects?: array<array-key, array{
* class: ReflectionClass,
* args: array,
* argNames?: array,
* argNames?: <array-key, string>,
* obj: object
* }>,
* scalars?: array
Expand Down Expand Up @@ -349,20 +349,14 @@ protected function gatherRowData(array $data, array &$id, array &$nonemptyCompon
$newObject = $rowData['newObjects'][$ownerIndex . ':' . $argIndex];
unset($rowData['newObjects'][$ownerIndex . ':' . $argIndex]);

$class = $newObject['class'];
$args = $newObject['args'];
$argColumnNames = $newObject['argColumnNames'];
$obj = $this->getNewObjectInstance($class, $args, $argColumnNames);
$obj = $this->getNewObjectInstance($newObject['class'], $newObject['args'], $newObject['argColumnNames']);

$rowData['newObjects'][$ownerIndex]['args'][$argIndex] = $obj;
$rowData['newObjects'][$ownerIndex]['argColumnNames'][$argIndex] = $objIndex;
}

foreach ($rowData['newObjects'] as $objIndex => $newObject) {
$class = $newObject['class'];
$args = $newObject['args'];
$argColumnNames = $newObject['argColumnNames'];
$obj = $this->getNewObjectInstance($class, $args, $argColumnNames);
$obj = $this->getNewObjectInstance($newObject['class'], $newObject['args'], $newObject['argColumnNames']);

$rowData['newObjects'][$objIndex]['obj'] = $obj;
}
Expand Down
8 changes: 6 additions & 2 deletions tests/Tests/Models/CMS/CmsAddressDTONamedArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

class CmsAddressDTONamedArgs implements WithNamedArguments
{
public function __construct(public string|null $country = null, public string|null $city = null, public string|null $zip = null, public CmsAddressDTO|string|null $address = null)
{
public function __construct(
public string|null $country = null,
public string|null $city = null,
public string|null $zip = null,
public CmsAddressDTO|string|null $address = null,
) {
}
}
10 changes: 8 additions & 2 deletions tests/Tests/Models/CMS/CmsUserDTONamedArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

class CmsUserDTONamedArgs implements WithNamedArguments
{
public function __construct(public string|null $name = null, public string|null $email = null, public string|null $address = null, public int|null $phonenumbers = null, public CmsAddressDTO|null $addressDto = null, public CmsAddressDTONamedArgs|null $addressDtoNamedArgs = null)
{
public function __construct(
public string|null $name = null,
public string|null $email = null,
public string|null $address = null,
public int|null $phonenumbers = null,
public CmsAddressDTO|null $addressDto = null,
public CmsAddressDTONamedArgs|null $addressDtoNamedArgs = null,
) {
}
}
72 changes: 60 additions & 12 deletions tests/Tests/ORM/Functional/NewOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPUnit\Framework\Attributes\Group;

use function count;
use function sprintf;

#[Group('DDC-1574')]
class NewOperatorTest extends OrmFunctionalTestCase
Expand Down Expand Up @@ -1086,12 +1087,12 @@ public function testShouldSupportNestedNewOperators(): void

public function testNamedArguments(): void
{
$dql = '
$dql = <<<'SQL'
SELECT
new CmsUserDTONamedArgs(
e.email,
u.name,
CONCAT(a.country, \' \', a.city, \' \', a.zip) AS address
CONCAT(a.country, ' ', a.city, ' ', a.zip) AS address
) as user,
u.status,
u.username as cmsUserUsername
Expand All @@ -1102,7 +1103,8 @@ public function testNamedArguments(): void
JOIN
u.address a
ORDER BY
u.name';
u.name
SQL;

$query = $this->getEntityManager()->createQuery($dql);
$result = $query->getResult();
Expand All @@ -1121,9 +1123,30 @@ public function testNamedArguments(): void
self::assertSame($this->fixtures[1]->email->email, $result[1]['user']->email);
self::assertSame($this->fixtures[2]->email->email, $result[2]['user']->email);

self::assertSame($this->fixtures[0]->address->country . ' ' . $this->fixtures[0]->address->city . ' ' . $this->fixtures[0]->address->zip, $result[0]['user']->address);
self::assertSame($this->fixtures[1]->address->country . ' ' . $this->fixtures[1]->address->city . ' ' . $this->fixtures[1]->address->zip, $result[1]['user']->address);
self::assertSame($this->fixtures[2]->address->country . ' ' . $this->fixtures[2]->address->city . ' ' . $this->fixtures[2]->address->zip, $result[2]['user']->address);
self::assertSame(sprintf(
'%s %s %s',
$this->fixtures[0]->address->country,
$this->fixtures[0]->address->city,
$this->fixtures[0]->address->zip,
), $result[0]['user']->address);
self::assertSame(
sprintf(
'%s %s %s',
$this->fixtures[1]->address->country,
$this->fixtures[1]->address->city,
$this->fixtures[1]->address->zip,
),
$result[1]['user']->address,
);
self::assertSame(
sprintf(
'%s %s %s',
$this->fixtures[2]->address->country,
$this->fixtures[2]->address->city,
$this->fixtures[2]->address->zip,
),
$result[2]['user']->address,
);

self::assertSame($this->fixtures[0]->status, $result[0]['status']);
self::assertSame($this->fixtures[1]->status, $result[1]['status']);
Expand All @@ -1136,10 +1159,10 @@ public function testNamedArguments(): void

public function testVariadicArgument(): void
{
$dql = '
$dql = <<<'SQL'
SELECT
new CmsUserDTOVariadicArg(
CONCAT(a.country, \' \', a.city, \' \', a.zip) AS address,
CONCAT(a.country, ' ', a.city, ' ', a.zip) AS address,
e.email,
u.name
) as user,
Expand All @@ -1152,7 +1175,8 @@ public function testVariadicArgument(): void
JOIN
u.address a
ORDER BY
u.name';
u.name
SQL;

$query = $this->getEntityManager()->createQuery($dql);
$result = $query->getResult();
Expand All @@ -1171,9 +1195,33 @@ public function testVariadicArgument(): void
self::assertSame($this->fixtures[1]->email->email, $result[1]['user']->email);
self::assertSame($this->fixtures[2]->email->email, $result[2]['user']->email);

self::assertSame($this->fixtures[0]->address->country . ' ' . $this->fixtures[0]->address->city . ' ' . $this->fixtures[0]->address->zip, $result[0]['user']->address);
self::assertSame($this->fixtures[1]->address->country . ' ' . $this->fixtures[1]->address->city . ' ' . $this->fixtures[1]->address->zip, $result[1]['user']->address);
self::assertSame($this->fixtures[2]->address->country . ' ' . $this->fixtures[2]->address->city . ' ' . $this->fixtures[2]->address->zip, $result[2]['user']->address);
self::assertSame(
sprintf(
'%s %s %s',
$this->fixtures[0]->address->country,
$this->fixtures[0]->address->city,
$this->fixtures[0]->address->zip,
),
$result[0]['user']->address,
);
self::assertSame(
sprintf(
'%s %s %s',
$this->fixtures[1]->address->country,
$this->fixtures[1]->address->city,
$this->fixtures[1]->address->zip,
),
$result[1]['user']->address,
);
self::assertSame(
sprintf(
'%s %s %s',
$this->fixtures[2]->address->country,
$this->fixtures[2]->address->city,
$this->fixtures[2]->address->zip,
),
$result[2]['user']->address,
);

self::assertSame($this->fixtures[0]->status, $result[0]['status']);
self::assertSame($this->fixtures[1]->status, $result[1]['status']);
Expand Down

0 comments on commit ce472bb

Please sign in to comment.