Skip to content

Commit ee0de03

Browse files
Fix check for where condition
1 parent a1a5322 commit ee0de03

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
@@ -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)