Skip to content

Commit 92e2f6d

Browse files
authored
Merge pull request #12012 from greg0ire/revert-allfields-dto
Revert "add capability to use allfields sql notation"
2 parents cc2b638 + aa624f6 commit 92e2f6d

File tree

9 files changed

+28
-438
lines changed

9 files changed

+28
-438
lines changed

UPGRADE.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Upgrade to 3.4.1
2+
3+
## BC BREAK: You can no longer use the `.*` notation to get all fields of an entity in a DTO
4+
5+
This feature was introduced in 3.4.0, and introduces several issues, so we
6+
decide to remove it before it is used too widely.
7+
18
# Upgrade to 3.4
29

310
## Discriminator Map class duplicates
@@ -26,7 +33,7 @@ The class `Doctrine\ORM\Mapping\Driver\DatabaseDriver` is deprecated without rep
2633

2734
Output walkers should implement the new `\Doctrine\ORM\Query\OutputWalker` interface and create
2835
`Doctrine\ORM\Query\Exec\SqlFinalizer` instances instead of `Doctrine\ORM\Query\Exec\AbstractSqlExecutor`s.
29-
The output walker must not base its workings on the query `firstResult`/`maxResult` values, so that the
36+
The output walker must not base its workings on the query `firstResult`/`maxResult` values, so that the
3037
`SqlFinalizer` can be kept in the query cache and used regardless of the actual `firstResult`/`maxResult` values.
3138
Any operation dependent on `firstResult`/`maxResult` should take place within the `SqlFinalizer::createExecutor()`
3239
method. Details can be found at https://github.com/doctrine/orm/pull/11188.
@@ -137,7 +144,7 @@ WARNING: This was relaxed in ORM 3.2 when partial was re-allowed for array-hydra
137144
`Doctrine\ORM\Query::HINT_FORCE_PARTIAL_LOAD` are removed.
138145
- `Doctrine\ORM\EntityManager*::getPartialReference()` is removed.
139146

140-
## BC BREAK: Enforce ArrayCollection Type on `\Doctrine\ORM\QueryBuilder::setParameters(ArrayCollection $parameters)`
147+
## BC BREAK: Enforce ArrayCollection Type on `\Doctrine\ORM\QueryBuilder::setParameters(ArrayCollection $parameters)`
141148

142149
The argument $parameters can no longer be a key=>value array. Only ArrayCollection types are allowed.
143150

docs/en/reference/dql-doctrine-query-language.rst

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ And then use the ``NEW`` DQL keyword :
588588
$query = $em->createQuery('SELECT NEW CustomerDTO(c.name, e.email, a.city, SUM(o.value)) FROM Customer c JOIN c.email e JOIN c.address a JOIN c.orders o GROUP BY c');
589589
$users = $query->getResult(); // array of CustomerDTO
590590
591-
You can also nest several DTO :
591+
You can also nest several DTO :
592592

593593
.. code-block:: php
594594
@@ -684,18 +684,6 @@ You can hydrate an entity nested in a DTO :
684684
685685
// CustomerDTO => {name : 'DOE', email: null, address : {city: 'New York', zip: '10011', address: 'Abbey Road'}
686686
687-
In a Dto, if you want add all fields of an entity, you can use ``.*`` :
688-
689-
.. code-block:: php
690-
691-
<?php
692-
$query = $em->createQuery('SELECT NEW NAMED CustomerDTO(c.name, NEW NAMED AddressDTO(a.*) AS address) FROM Customer c JOIN c.address a');
693-
$users = $query->getResult(); // array of CustomerDTO
694-
695-
// CustomerDTO => {name : 'DOE', email: null, city: null, address: {id: 18, city: 'New York', zip: '10011'}}
696-
697-
It's recommended to use named arguments DTOs with the ``.*`` notation because argument order is not guaranteed.
698-
699687
Using INDEX BY
700688
~~~~~~~~~~~~~~
701689

@@ -1719,14 +1707,13 @@ Select Expressions
17191707

17201708
.. code-block:: php
17211709
1722-
SelectExpression ::= (IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression | NewObjectExpression) [["AS"] ["HIDDEN"] AliasResultVariable]
1723-
SimpleSelectExpression ::= (StateFieldPathExpression | IdentificationVariable | FunctionDeclaration | AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable]
1710+
SelectExpression ::= (IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression | NewObjectExpression) [["AS"] ["HIDDEN"] AliasResultVariable]
1711+
SimpleSelectExpression ::= (StateFieldPathExpression | IdentificationVariable | FunctionDeclaration | AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable]
17241712
PartialObjectExpression ::= "PARTIAL" IdentificationVariable "." PartialFieldSet
17251713
PartialFieldSet ::= "{" SimpleStateField {"," SimpleStateField}* "}"
17261714
NewObjectExpression ::= "NEW" AbstractSchemaName "(" NewObjectArg {"," NewObjectArg}* ")"
1727-
NewObjectArg ::= ((ScalarExpression | "(" Subselect ")" | NewObjectExpression | EntityAsDtoArgumentExpression) ["AS" AliasResultVariable]) | AllFieldsExpression
1715+
NewObjectArg ::= (ScalarExpression | "(" Subselect ")" | NewObjectExpression | EntityAsDtoArgumentExpression) ["AS" AliasResultVariable]
17281716
EntityAsDtoArgumentExpression ::= IdentificationVariable
1729-
AllFieldsExpression ::= IdentificationVariable ".*"
17301717
17311718
Conditional Expressions
17321719
~~~~~~~~~~~~~~~~~~~~~~~

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2811,7 +2811,7 @@ parameters:
28112811
-
28122812
message: '#^Cannot assign new offset to list\<string\>\|string\.$#'
28132813
identifier: offsetAssign.dimType
2814-
count: 3
2814+
count: 2
28152815
path: src/Query/SqlWalker.php
28162816

28172817
-

src/Query/AST/AllFieldsExpression.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Query/AST/NewObjectExpression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class NewObjectExpression extends Node
2020
* @param class-string $className
2121
* @param mixed[] $args
2222
*/
23-
public function __construct(public string $className, public array $args, public bool $hasNamedArgs = false)
23+
public function __construct(public string $className, public array $args)
2424
{
2525
}
2626

src/Query/Parser.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,6 @@ public function PathExpression(int $expectedTypes): AST\PathExpression
10361036
assert($this->lexer->token !== null);
10371037
if ($this->lexer->isNextToken(TokenType::T_DOT)) {
10381038
$this->match(TokenType::T_DOT);
1039-
10401039
$this->match(TokenType::T_IDENTIFIER);
10411040

10421041
$field = $this->lexer->token->value;
@@ -1151,20 +1150,6 @@ public function EntityAsDtoArgumentExpression(): AST\EntityAsDtoArgumentExpressi
11511150
return new AST\EntityAsDtoArgumentExpression($expression, $identVariable);
11521151
}
11531152

1154-
/**
1155-
* AllFieldsExpression ::= IdentificationVariable
1156-
*/
1157-
public function AllFieldsExpression(): AST\AllFieldsExpression
1158-
{
1159-
$identVariable = $this->IdentificationVariable();
1160-
assert($this->lexer->token !== null);
1161-
1162-
$this->match(TokenType::T_DOT);
1163-
$this->match(TokenType::T_MULTIPLY);
1164-
1165-
return new AST\AllFieldsExpression($identVariable);
1166-
}
1167-
11681153
/**
11691154
* SelectClause ::= "SELECT" ["DISTINCT"] SelectExpression {"," SelectExpression}
11701155
*/
@@ -1841,7 +1826,7 @@ public function NewObjectExpression(): AST\NewObjectExpression
18411826

18421827
$this->match(TokenType::T_CLOSE_PARENTHESIS);
18431828

1844-
$expression = new AST\NewObjectExpression($className, $args, $useNamedArguments);
1829+
$expression = new AST\NewObjectExpression($className, $args);
18451830

18461831
// Defer NewObjectExpression validation
18471832
$this->deferredNewObjectExpressions[] = [
@@ -1888,7 +1873,7 @@ public function addArgument(array &$args, bool $useNamedArguments): void
18881873
}
18891874

18901875
/**
1891-
* NewObjectArg ::= ((ScalarExpression | "(" Subselect ")" | NewObjectExpression) ["AS" AliasResultVariable]) | AllFieldsExpression
1876+
* NewObjectArg ::= (ScalarExpression | "(" Subselect ")" | NewObjectExpression) ["AS" AliasResultVariable]
18921877
*/
18931878
public function NewObjectArg(string|null &$fieldAlias = null): mixed
18941879
{
@@ -2000,14 +1985,10 @@ public function ScalarExpression(): mixed
20001985
// it is no function, so it must be a field path
20011986
case $lookahead === TokenType::T_IDENTIFIER:
20021987
$this->lexer->peek(); // lookahead => '.'
2003-
$token = $this->lexer->peek(); // lookahead => token after '.'
2004-
$peek = $this->lexer->peek(); // lookahead => token after the token after the '.'
1988+
$this->lexer->peek(); // lookahead => token after '.'
1989+
$peek = $this->lexer->peek(); // lookahead => token after the token after the '.'
20051990
$this->lexer->resetPeek();
20061991

2007-
if ($token->value === '*') {
2008-
return $this->AllFieldsExpression();
2009-
}
2010-
20111992
if ($this->isMathOperator($peek)) {
20121993
return $this->SimpleArithmeticExpression();
20131994
}

src/Query/SqlWalker.php

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,17 +1518,11 @@ public function walkNewObject(AST\NewObjectExpression $newObjectExpression, stri
15181518
{
15191519
$sqlSelectExpressions = [];
15201520
$objIndex = $newObjectResultAlias ?: $this->newObjectCounter++;
1521-
$aliasGap = $newObjectExpression->hasNamedArgs ? null : 0;
15221521

15231522
foreach ($newObjectExpression->args as $argIndex => $e) {
1524-
if (! $newObjectExpression->hasNamedArgs) {
1525-
$argIndex += $aliasGap;
1526-
}
1527-
1528-
$resultAlias = $this->scalarResultCounter++;
1529-
$columnAlias = $this->getSQLColumnAlias('sclr');
1530-
$fieldType = 'string';
1531-
$isScalarResult = true;
1523+
$resultAlias = $this->scalarResultCounter++;
1524+
$columnAlias = $this->getSQLColumnAlias('sclr');
1525+
$fieldType = 'string';
15321526

15331527
switch (true) {
15341528
case $e instanceof AST\NewObjectExpression:
@@ -1582,26 +1576,18 @@ public function walkNewObject(AST\NewObjectExpression $newObjectExpression, stri
15821576
$sqlSelectExpressions[] = trim($e->dispatch($this));
15831577
break;
15841578

1585-
case $e instanceof AST\AllFieldsExpression:
1586-
$isScalarResult = false;
1587-
$sqlSelectExpressions[] = $e->dispatch($this, $objIndex, $argIndex, $aliasGap);
1588-
break;
1589-
15901579
default:
15911580
$sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
15921581
break;
15931582
}
15941583

1595-
if ($isScalarResult) {
1596-
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
1597-
$this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
1584+
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
1585+
$this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
15981586

1599-
$this->rsm->newObjectMappings[$columnAlias] = [
1600-
'className' => $newObjectExpression->className,
1601-
'objIndex' => $objIndex,
1602-
'argIndex' => $argIndex,
1603-
];
1604-
}
1587+
$this->rsm->newObjectMappings[$columnAlias] = [
1588+
'objIndex' => $objIndex,
1589+
'argIndex' => $argIndex,
1590+
];
16051591
}
16061592

16071593
$this->rsm->newObject[$objIndex] = $newObjectExpression->className;
@@ -2306,42 +2292,6 @@ public function walkResultVariable(string $resultVariable): string
23062292
return $resultAlias;
23072293
}
23082294

2309-
public function walkAllEntityFieldsExpression(AST\AllFieldsExpression $expression, int|string $objIndex, int|string $argIndex, int|null &$aliasGap): string
2310-
{
2311-
$dqlAlias = $expression->identificationVariable;
2312-
$class = $this->getMetadataForDqlAlias($expression->identificationVariable);
2313-
2314-
$sqlParts = [];
2315-
// Select all fields from the queried class
2316-
foreach ($class->fieldMappings as $fieldName => $mapping) {
2317-
$tableName = isset($mapping->inherited)
2318-
? $this->em->getClassMetadata($mapping->inherited)->getTableName()
2319-
: $class->getTableName();
2320-
2321-
$sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias);
2322-
$columnAlias = $this->getSQLColumnAlias($mapping->columnName);
2323-
$quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
2324-
2325-
$col = $sqlTableAlias . '.' . $quotedColumnName;
2326-
2327-
$type = Type::getType($mapping->type);
2328-
$col = $type->convertToPHPValueSQL($col, $this->platform);
2329-
2330-
$sqlParts[] = $col . ' AS ' . $columnAlias;
2331-
2332-
$this->scalarResultAliasMap[$objIndex][] = $columnAlias;
2333-
2334-
$this->rsm->addScalarResult($columnAlias, $objIndex, $mapping->type);
2335-
2336-
$this->rsm->newObjectMappings[$columnAlias] = [
2337-
'objIndex' => $objIndex,
2338-
'argIndex' => $aliasGap === null ? $fieldName : (int) $argIndex + $aliasGap++,
2339-
];
2340-
}
2341-
2342-
return implode(', ', $sqlParts);
2343-
}
2344-
23452295
/**
23462296
* @return string The list in parentheses of valid child discriminators from the given class
23472297
*

tests/Tests/Models/CMS/CmsDumbVariadicDTO.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)