Skip to content

Commit a921d28

Browse files
Fix check for where condition
1 parent 0e9df96 commit a921d28

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/Type/Doctrine/Query/QueryResultTypeWalker.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class QueryResultTypeWalker extends SqlWalker
109109
private $hasGroupByClause;
110110

111111
/** @var bool */
112-
private $hasCondition;
112+
private $hasWhereClause;
113113

114114
/**
115115
* @param Query<mixed> $query
@@ -139,7 +139,7 @@ public function __construct($query, $parserResult, array $queryComponents)
139139
$this->nullableQueryComponents = [];
140140
$this->hasAggregateFunction = false;
141141
$this->hasGroupByClause = false;
142-
$this->hasCondition = false;
142+
$this->hasWhereClause = false;
143143

144144
// The object is instantiated by Doctrine\ORM\Query\Parser, so receiving
145145
// dependencies through the constructor is not an option. Instead, we
@@ -179,6 +179,7 @@ public function walkSelectStatement(AST\SelectStatement $AST): string
179179
$this->typeBuilder->setSelectQuery();
180180
$this->hasAggregateFunction = $this->hasAggregateFunction($AST);
181181
$this->hasGroupByClause = $AST->groupByClause !== null;
182+
$this->hasWhereClause = $AST->whereClause !== null;
182183

183184
$this->walkFromClause($AST->fromClause);
184185

@@ -596,8 +597,6 @@ public function walkOrderByItem($orderByItem): string
596597
*/
597598
public function walkHavingClause($havingClause): string
598599
{
599-
$this->hasCondition = true;
600-
601600
return $this->marshalType(new MixedType());
602601
}
603602

@@ -1033,8 +1032,6 @@ public function walkWhereClause($whereClause): string
10331032
*/
10341033
public function walkConditionalExpression($condExpr): string
10351034
{
1036-
$this->hasCondition = true;
1037-
10381035
return $this->marshalType(new MixedType());
10391036
}
10401037

@@ -1322,10 +1319,10 @@ public function walkResultVariable($resultVariable): string
13221319
*/
13231320
private function addScalar($alias, Type $type): void
13241321
{
1325-
// Since we don't check the condition inside the WHERE or HAVING
1322+
// Since we don't check the condition inside the WHERE
13261323
// conditions, we cannot be sure all the union types are correct.
13271324
// For exemple, a condition `WHERE foo.bar IS NOT NULL` could be added.
1328-
if ($this->hasCondition && $type instanceof UnionType) {
1325+
if ($this->hasWhereClause && $type instanceof UnionType) {
13291326
$type = TypeUtils::toBenevolentUnion($type);
13301327
}
13311328

tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,25 @@ public function getTestData(): iterable
449449
',
450450
];
451451

452+
yield 'scalar with where condition' => [
453+
$this->constantArray([
454+
[new ConstantStringType('intColumn'), new IntegerType()],
455+
[new ConstantStringType('stringColumn'), new StringType()],
456+
[
457+
new ConstantStringType('stringNullColumn'),
458+
TypeUtils::toBenevolentUnion(TypeCombinator::addNull(new StringType()))
459+
],
460+
[new ConstantStringType('datetimeColumn'), new ObjectType(DateTime::class)],
461+
[new ConstantStringType('datetimeImmutableColumn'), new ObjectType(DateTimeImmutable::class)],
462+
]),
463+
'
464+
SELECT m.intColumn, m.stringColumn, m.stringNullColumn,
465+
m.datetimeColumn, m.datetimeImmutableColumn
466+
FROM QueryResult\Entities\Many m
467+
WHERE m.stringNullColumn IS NOT NULL
468+
',
469+
];
470+
452471
yield 'scalar with alias' => [
453472
$this->constantArray([
454473
[new ConstantStringType('i'), new IntegerType()],

0 commit comments

Comments
 (0)