Skip to content

Commit b8d290e

Browse files
Fix check for where condition
1 parent 3a2153a commit b8d290e

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/Type/Doctrine/Query/QueryResultTypeWalker.php

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

110110
/** @var bool */
111-
private $hasCondition;
111+
private $hasWhereClause;
112112

113113
/**
114114
* @param Query<mixed> $query
@@ -138,7 +138,7 @@ public function __construct($query, $parserResult, array $queryComponents)
138138
$this->nullableQueryComponents = [];
139139
$this->hasAggregateFunction = false;
140140
$this->hasGroupByClause = false;
141-
$this->hasCondition = false;
141+
$this->hasWhereClause = false;
142142

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

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

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

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

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

tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

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

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

0 commit comments

Comments
 (0)