@@ -107,6 +107,9 @@ class QueryResultTypeWalker extends SqlWalker
107
107
/** @var bool */
108
108
private $ hasGroupByClause ;
109
109
110
+ /** @var bool */
111
+ private $ hasCondition ;
112
+
110
113
/**
111
114
* @param Query<mixed> $query
112
115
*/
@@ -135,6 +138,7 @@ public function __construct($query, $parserResult, array $queryComponents)
135
138
$ this ->nullableQueryComponents = [];
136
139
$ this ->hasAggregateFunction = false ;
137
140
$ this ->hasGroupByClause = false ;
141
+ $ this ->hasCondition = false ;
138
142
139
143
// The object is instantiated by Doctrine\ORM\Query\Parser, so receiving
140
144
// dependencies through the constructor is not an option. Instead, we
@@ -590,6 +594,8 @@ public function walkOrderByItem($orderByItem)
590
594
*/
591
595
public function walkHavingClause ($ havingClause )
592
596
{
597
+ $ this ->hasCondition = true ;
598
+
593
599
return $ this ->marshalType (new MixedType ());
594
600
}
595
601
@@ -994,6 +1000,8 @@ public function walkUpdateItem($updateItem)
994
1000
*/
995
1001
public function walkWhereClause ($ whereClause )
996
1002
{
1003
+ $ this ->hasCondition = true ;
1004
+
997
1005
return $ this ->marshalType (new MixedType ());
998
1006
}
999
1007
@@ -1281,7 +1289,10 @@ public function walkResultVariable($resultVariable)
1281
1289
*/
1282
1290
private function addScalar ($ alias , Type $ type ): void
1283
1291
{
1284
- if ($ type instanceof UnionType) {
1292
+ // Since we don't check the condition inside the WHERE or HAVING
1293
+ // conditions, we cannot be sure all the union types are correct.
1294
+ // For exemple, a condition `WHERE foo.bar IS NOT NULL` could be added.
1295
+ if ($ this ->hasCondition && $ type instanceof UnionType) {
1285
1296
$ type = TypeUtils::toBenevolentUnion ($ type );
1286
1297
}
1287
1298
0 commit comments