From a522617e2a549e8165671be5a78586e885db66f4 Mon Sep 17 00:00:00 2001
From: Dominique Kreemers <dominique.kreemers@icloud.com>
Date: Tue, 17 Jan 2017 12:19:24 +0100
Subject: [PATCH] [BUGFIX] Fix early return in addUnionStatement method

Set $propertyPath, $tableName and $className to avoid
unnecessary joins and wrong repository results.

This is a regression fix after the Doctrine migration.

Resolves: #79286
Related: #77379
Releases: master
Change-Id: I64489b46b9ca751535b64482f54ceb7b2da05003
Reviewed-on: https://review.typo3.org/51290
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
---
 .../Persistence/Generic/Storage/Typo3DbQueryParser.php      | 5 ++++-
 .../extbase/Tests/Functional/Persistence/CountTest.php      | 6 +++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbQueryParser.php b/typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbQueryParser.php
index 8dc9259b6955..fb90ddb40e02 100644
--- a/typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbQueryParser.php
+++ b/typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbQueryParser.php
@@ -905,8 +905,11 @@ class Typo3DbQueryParser
         $fullPropertyPath .= ($fullPropertyPath === '') ? $propertyName : '.' . $propertyName;
         $childTableAlias = $this->getUniqueAlias($childTableName, $fullPropertyPath);
 
-        // If there is already exists a union with the current identifier we do not need to build it again and exit early.
+        // If there is already a union with the current identifier we do not need to build it again and exit early.
         if (in_array($childTableAlias, $this->unionTableAliasCache, true)) {
+            $propertyPath = $explodedPropertyPath[1];
+            $tableName = $childTableAlias;
+            $className = $this->dataMapper->getType($className, $propertyName);
             return;
         }
 
diff --git a/typo3/sysext/extbase/Tests/Functional/Persistence/CountTest.php b/typo3/sysext/extbase/Tests/Functional/Persistence/CountTest.php
index 87a5367a3eb4..79ab879e2ef2 100644
--- a/typo3/sysext/extbase/Tests/Functional/Persistence/CountTest.php
+++ b/typo3/sysext/extbase/Tests/Functional/Persistence/CountTest.php
@@ -197,7 +197,11 @@ class CountTest extends \TYPO3\CMS\Components\TestingFramework\Core\FunctionalTe
             )
         );
 
-        $this->assertSame(10, $query->count());
+        // QueryResult is lazy, so we have to run valid method to initialize
+        $result = $query->execute();
+        $result->valid();
+
+        $this->assertSame(10, $result->count());
     }
 
     /**
-- 
GitLab