From 1f81eac355bf073f8deba6dab7bee5544abbda15 Mon Sep 17 00:00:00 2001
From: Helmut Hummel <typo3@helhum.io>
Date: Fri, 21 Jul 2017 17:10:05 +0200
Subject: [PATCH] [BUGFIX] Do not detect single select fields as relation

With https://review.typo3.org/50879/ all TCA select fields
are detected as relation to many, even when they are configured
as simple static select.

This leads to the row value to be pre-set as string (to hold comma
separated values), which leads to an exception in MySQL Strict Mode
when the select value field is defined as integer.

Therefore only detect renderType === selectMultipleSideBySide
as relational field

Resolves: #81043
Releases: master, 8.7
Change-Id: I54effaace3eb19034d86a9a74bbd5cc207e867f1
Reviewed-on: https://review.typo3.org/53565
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Reviewed-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
Tested-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
---
 .../Generic/Mapper/DataMapFactory.php         |  9 ++++++-
 .../Generic/Mapper/DataMapFactoryTest.php     | 24 +++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapFactory.php b/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapFactory.php
index 41138248745b..1ae050418021 100644
--- a/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapFactory.php
+++ b/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapFactory.php
@@ -330,7 +330,14 @@ class DataMapFactory implements \TYPO3\CMS\Core\SingletonInterface
                 $columnMap = $this->setOneToManyRelation($columnMap, $columnConfiguration);
             } elseif (isset($propertyMetaData['type']) && strpbrk($propertyMetaData['type'], '_\\') !== false) {
                 $columnMap = $this->setOneToOneRelation($columnMap, $columnConfiguration);
-            } elseif (isset($columnConfiguration['type']) && $columnConfiguration['type'] === 'select') {
+            } elseif (
+                isset($columnConfiguration['type'], $columnConfiguration['renderType'])
+                && $columnConfiguration['type'] === 'select'
+                && (
+                    $columnConfiguration['renderType'] !== 'selectSingle'
+                    || (isset($columnConfiguration['maxitems']) && $columnConfiguration['maxitems'] > 1)
+                )
+            ) {
                 $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_MANY);
             } else {
                 $columnMap->setTypeOfRelation(ColumnMap::RELATION_NONE);
diff --git a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Mapper/DataMapFactoryTest.php b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Mapper/DataMapFactoryTest.php
index f6922c2189b9..977ffa92a203 100644
--- a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Mapper/DataMapFactoryTest.php
+++ b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Mapper/DataMapFactoryTest.php
@@ -150,6 +150,30 @@ class DataMapFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
     }
 
+    /**
+     * @test
+     */
+    public function setRelationsDetectsSelectRenderTypeSingleAsNonRelational()
+    {
+        $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('foo', 'foo');
+        $columnConfiguration = [
+            'type' => 'select',
+            'renderType' => 'selectSingle',
+            'items' => [
+                ['One', 1],
+                ['Two', 2],
+                ['Three', 3],
+            ],
+        ];
+        $propertyMetaData = [];
+        $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'], [], '', false);
+        $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
+        $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
+        $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
+        $actualColumnMap = $mockDataMapFactory->_callRef('setRelations', $columnMap, $columnConfiguration, $propertyMetaData);
+        $this->assertSame($columnMap::RELATION_NONE, $actualColumnMap->getTypeOfRelation());
+    }
+
     /**
      * @test
      */
-- 
GitLab