@@ -106,6 +106,9 @@ class QueryResultTypeWalker extends SqlWalker
106106 /** @var bool */
107107 private $ hasGroupByClause ;
108108
109+ /** @var bool */
110+ private $ hasCondition ;
111+
109112 /**
110113 * @param Query<mixed> $query
111114 */
@@ -134,6 +137,7 @@ public function __construct($query, $parserResult, array $queryComponents)
134137 $ this ->nullableQueryComponents = [];
135138 $ this ->hasAggregateFunction = false ;
136139 $ this ->hasGroupByClause = false ;
140+ $ this ->hasCondition = false ;
137141
138142 // The object is instantiated by Doctrine\ORM\Query\Parser, so receiving
139143 // dependencies through the constructor is not an option. Instead, we
@@ -589,6 +593,8 @@ public function walkOrderByItem($orderByItem)
589593 */
590594 public function walkHavingClause ($ havingClause )
591595 {
596+ $ this ->hasCondition = true ;
597+
592598 return $ this ->marshalType (new MixedType ());
593599 }
594600
@@ -993,6 +999,8 @@ public function walkUpdateItem($updateItem)
993999 */
9941000 public function walkWhereClause ($ whereClause )
9951001 {
1002+ $ this ->hasCondition = true ;
1003+
9961004 return $ this ->marshalType (new MixedType ());
9971005 }
9981006
@@ -1280,7 +1288,7 @@ public function walkResultVariable($resultVariable)
12801288 */
12811289 private function addScalar ($ alias , Type $ type ): void
12821290 {
1283- if ($ type instanceof UnionType) {
1291+ if ($ this -> hasCondition && $ type instanceof UnionType) {
12841292 $ type = TypeUtils::toBenevolentUnion ($ type );
12851293 }
12861294
0 commit comments