From b10285e78473c5fb65a2e3dd60828f5950d9d9b5 Mon Sep 17 00:00:00 2001
From: Markus Klein <markus.klein@typo3.org>
Date: Tue, 10 Jan 2023 13:31:26 +0100
Subject: [PATCH] [BUGFIX] Do not query database for non-existent records
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Extbase does not ask the database anymore for relations, where the uid
in the database field is zero.

This prevents SQL queries with where-parts like `uid IN (0)`.

Since this is done when reconstituting models from the database,
depending on the amount of relations of a model, the number of saved
queries is huge.

Resolves: #99505
Releases: main, 11.5
Change-Id: If3b6c2756674eeb43a66b59a13e44011c9845cd5
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/77582
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Stephan Großberndt <stephan.grossberndt@typo3.org>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Stephan Großberndt <stephan.grossberndt@typo3.org>
---
 .../Persistence/Generic/Mapper/DataMapper.php | 26 ++++++++-----------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php b/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php
index d48b9dcecad1..d86ea769f26d 100644
--- a/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php
+++ b/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php
@@ -322,15 +322,13 @@ class DataMapper
     public function fetchRelated(DomainObjectInterface $parentObject, $propertyName, $fieldValue = '', $enableLazyLoading = true)
     {
         $property = $this->reflectionService->getClassSchema(get_class($parentObject))->getProperty($propertyName);
-        if ($enableLazyLoading === true && $property->isLazy()) {
+        if ($enableLazyLoading && $property->isLazy()) {
             if ($property->getType() === ObjectStorage::class) {
                 $result = GeneralUtility::makeInstance(LazyObjectStorage::class, $parentObject, $propertyName, $fieldValue, $this);
+            } elseif (empty($fieldValue)) {
+                $result = null;
             } else {
-                if (empty($fieldValue)) {
-                    $result = null;
-                } else {
-                    $result = GeneralUtility::makeInstance(LazyLoadingProxy::class, $parentObject, $propertyName, $fieldValue, $this);
-                }
+                $result = GeneralUtility::makeInstance(LazyLoadingProxy::class, $parentObject, $propertyName, $fieldValue, $this);
             }
         } else {
             $result = $this->fetchRelatedEager($parentObject, $propertyName, $fieldValue);
@@ -620,17 +618,15 @@ class DataMapper
         if ($this->propertyMapsByForeignKey($parentObject, $propertyName)) {
             $result = $this->fetchRelated($parentObject, $propertyName, $fieldValue);
             $propertyValue = $this->mapResultToPropertyValue($parentObject, $propertyName, $result);
+        } elseif (empty($fieldValue)) {
+            $propertyValue = $this->getEmptyRelationValue($parentObject, $propertyName);
         } else {
-            if ($fieldValue === '') {
-                $propertyValue = $this->getEmptyRelationValue($parentObject, $propertyName);
+            $property = $this->reflectionService->getClassSchema(get_class($parentObject))->getProperty($propertyName);
+            if ($this->persistenceSession->hasIdentifier($fieldValue, $property->getType())) {
+                $propertyValue = $this->persistenceSession->getObjectByIdentifier($fieldValue, $property->getType());
             } else {
-                $property = $this->reflectionService->getClassSchema(get_class($parentObject))->getProperty($propertyName);
-                if ($this->persistenceSession->hasIdentifier($fieldValue, $property->getType())) {
-                    $propertyValue = $this->persistenceSession->getObjectByIdentifier($fieldValue, $property->getType());
-                } else {
-                    $result = $this->fetchRelated($parentObject, $propertyName, $fieldValue);
-                    $propertyValue = $this->mapResultToPropertyValue($parentObject, $propertyName, $result);
-                }
+                $result = $this->fetchRelated($parentObject, $propertyName, $fieldValue);
+                $propertyValue = $this->mapResultToPropertyValue($parentObject, $propertyName, $result);
             }
         }
 
-- 
GitLab