Skip to content

Commit 7382b34

Browse files
Fix check for where condition
1 parent 2ef94e1 commit 7382b34

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

Diff for: src/Type/Doctrine/Query/QueryResultTypeWalker.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class QueryResultTypeWalker extends SqlWalker
107107
private $hasGroupByClause;
108108

109109
/** @var bool */
110-
private $hasCondition;
110+
private $hasWhereClause;
111111

112112
/**
113113
* @param Query<mixed> $query
@@ -137,7 +137,7 @@ public function __construct($query, $parserResult, array $queryComponents)
137137
$this->nullableQueryComponents = [];
138138
$this->hasAggregateFunction = false;
139139
$this->hasGroupByClause = false;
140-
$this->hasCondition = false;
140+
$this->hasWhereClause = false;
141141

142142
// The object is instantiated by Doctrine\ORM\Query\Parser, so receiving
143143
// dependencies through the constructor is not an option. Instead, we
@@ -180,6 +180,7 @@ public function walkSelectStatement(AST\SelectStatement $AST)
180180
$this->typeBuilder->setSelectQuery();
181181
$this->hasAggregateFunction = $this->hasAggregateFunction($AST);
182182
$this->hasGroupByClause = $AST->groupByClause !== null;
183+
$this->hasWhereClause = $AST->whereClause !== null;
183184

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

@@ -593,8 +594,6 @@ public function walkOrderByItem($orderByItem)
593594
*/
594595
public function walkHavingClause($havingClause)
595596
{
596-
$this->hasCondition = true;
597-
598597
return $this->marshalType(new MixedType());
599598
}
600599

@@ -1010,8 +1009,6 @@ public function walkUpdateItem($updateItem)
10101009
*/
10111010
public function walkWhereClause($whereClause)
10121011
{
1013-
$this->hasCondition = true;
1014-
10151012
return $this->marshalType(new MixedType());
10161013
}
10171014

@@ -1299,10 +1296,10 @@ public function walkResultVariable($resultVariable)
12991296
*/
13001297
private function addScalar($alias, Type $type): void
13011298
{
1302-
// Since we don't check the condition inside the WHERE or HAVING
1299+
// Since we don't check the condition inside the WHERE
13031300
// conditions, we cannot be sure all the union types are correct.
13041301
// For exemple, a condition `WHERE foo.bar IS NOT NULL` could be added.
1305-
if ($this->hasCondition && $type instanceof UnionType) {
1302+
if ($this->hasWhereClause && $type instanceof UnionType) {
13061303
$type = TypeUtils::toBenevolentUnion($type);
13071304
}
13081305

Diff for: tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,25 @@ public function getTestData(): iterable
441441
',
442442
];
443443

444+
yield 'scalar with where condition' => [
445+
$this->constantArray([
446+
[new ConstantStringType('intColumn'), new IntegerType()],
447+
[new ConstantStringType('stringColumn'), new StringType()],
448+
[
449+
new ConstantStringType('stringNullColumn'),
450+
TypeUtils::toBenevolentUnion(TypeCombinator::addNull(new StringType()))
451+
],
452+
[new ConstantStringType('datetimeColumn'), new ObjectType(DateTime::class)],
453+
[new ConstantStringType('datetimeImmutableColumn'), new ObjectType(DateTimeImmutable::class)],
454+
]),
455+
'
456+
SELECT m.intColumn, m.stringColumn, m.stringNullColumn,
457+
m.datetimeColumn, m.datetimeImmutableColumn
458+
FROM QueryResult\Entities\Many m
459+
WHERE m.stringNullColumn IS NOT NULL
460+
',
461+
];
462+
444463
yield 'scalar with alias' => [
445464
$this->constantArray([
446465
[new ConstantStringType('i'), new IntegerType()],

0 commit comments

Comments
 (0)