Skip to content

Commit

Permalink
Changes requested in code review
Browse files Browse the repository at this point in the history
Change unnamed arguments wording to "ordered arguments" instead
Change docs wording
  • Loading branch information
Zuruuh committed Dec 14, 2023
1 parent d9a2dd1 commit c06a583
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/en/reference/dql-doctrine-query-language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -584,15 +584,15 @@ And then use the ``NEW`` DQL keyword :
Note that you can only pass scalar expressions to the constructor.

The ``NEW`` operator also supports named arguments, similarly to php 8.0 :
The ``NEW`` operator also supports named arguments:

.. code-block:: php
<?php
$query = $em->createQuery('SELECT NEW CustomerDTO(email: e.email, name: c.name, address: a.city) FROM Customer c JOIN c.email e JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
Note that you cannot pass unnamed arguments after named ones, just like in php.
Note that you must not pass ordered arguments after named ones.

Using INDEX BY
~~~~~~~~~~~~~~
Expand Down
10 changes: 5 additions & 5 deletions lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,22 +573,22 @@ protected function hydrateRowData(array $row, array &$result)
$obj = $class->newInstanceArgs($args);
} else {
$constructor = $class->getConstructor();
$unnamedArgs = [];
$orderedArgs = [];

$constructorArguments = $constructor->getParameters();
$constructorArgumentsCount = count($constructorArguments);

foreach ($constructorArguments as $argument) {
if (array_key_exists($argument->getName(), $args)) {
$unnamedArgs[$argument->getPosition()] = $args[$argument->getName()];
$orderedArgs[$argument->getPosition()] = $args[$argument->getName()];
} elseif (array_key_exists($argument->getPosition(), $args)) {
$unnamedArgs[$argument->getPosition()] = $args[$argument->getPosition()];
$orderedArgs[$argument->getPosition()] = $args[$argument->getPosition()];
} else {
$unnamedArgs[$argument->getPosition()] = $argument->getDefaultValue();
$orderedArgs[$argument->getPosition()] = $argument->getDefaultValue();
}
}

$obj = $class->newInstanceArgs($unnamedArgs);
$obj = $class->newInstanceArgs($orderedArgs);
}

if ($scalarCount === 0 && count($rowData['newObjects']) === 1) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/AST/NamedScalarExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NamedScalarExpression extends Node
/** @var Node */
public $innerExpression;

/** @var ?string */
/** @var string|null */
public $name;

public function __construct(Node $scalarExpression, ?string $name = null)
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,7 @@ public function NewObjectArg(/* bool $namedArgAlreadyParsed = false */)
}

if ($namedArgAlreadyParsed) {
throw QueryException::syntaxError('Cannot specify unnamed arguments after named ones.');
throw QueryException::syntaxError('Cannot specify ordered arguments after named ones.');
}

if ($token->type === Lexer::T_OPEN_PARENTHESIS && $peek->type === Lexer::T_SELECT) {
Expand Down
8 changes: 4 additions & 4 deletions tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ public static function provideQueriesWithNamedArguments(): array
ORDER BY
u.name',
],
'Both named and unnamed arguments' => [
'Both named and ordered arguments' => [
'SELECT
new Doctrine\Tests\Models\CMS\CmsUserDTO(
u.name,
Expand All @@ -1128,7 +1128,7 @@ public static function provideQueriesWithNamedArguments(): array
ORDER BY
u.name',
],
'Both named and unnamed arguments without trailing comma' => [
'Both named and ordered arguments without trailing comma' => [
'SELECT
new Doctrine\Tests\Models\CMS\CmsUserDTO(
u.name,
Expand Down Expand Up @@ -1176,7 +1176,7 @@ public function testQueryWithNamedArguments(string $query): void
self::assertNull($result[2]->phonenumbers);
}

public function testQueryWithUnnamedArgumentAfterNamedArgument(): void
public function testQueryWithOrderedArgumentAfterNamedArgument(): void
{
$dql = '
SELECT
Expand All @@ -1196,7 +1196,7 @@ public function testQueryWithUnnamedArgumentAfterNamedArgument(): void

$query = $this->_em->createQuery($dql);
$this->expectException(QueryException::class);
$this->expectExceptionMessage('[Syntax Error] Cannot specify unnamed arguments after named ones.');
$this->expectExceptionMessage('[Syntax Error] Cannot specify ordered arguments after named ones.');

$query->getResult();
}
Expand Down

0 comments on commit c06a583

Please sign in to comment.