From 29dbc2fb66d855ecd95bd73cefc4768ef25f5e3f Mon Sep 17 00:00:00 2001
From: Nicole Cordes <typo3@cordes.co>
Date: Fri, 7 Oct 2016 13:58:00 +0200
Subject: [PATCH] [BUGFIX] Remove relations from MM table

If extbase deletes an object it doesn't delete its relations in an
MM table. This patch adds the check for a possible MM relation table and
ensures all relations get deleted as well. If the parent table supports
a deleted flag, no relations will be deleted to be able to restore the
parent record again.

Resolves: #78128
Releases: master, 7.6
Change-Id: Ibe2497c05838e261f9c67a80216385d6e60607cb
Reviewed-on: https://review.typo3.org/50103
Tested-by: TYPO3com <no-reply@typo3.com>
Tested-by: Robert Jelinek <robert.jelinek@riir.at>
Reviewed-by: Alexander Stehlik <alexander.stehlik@gmail.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
---
 .../sysext/extbase/Classes/Persistence/Generic/Backend.php | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php b/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php
index e7cba2f21d5b..c27076b69dc9 100644
--- a/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php
+++ b/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php
@@ -1092,6 +1092,9 @@ class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
         $properties = $object->_getProperties();
         foreach ($properties as $propertyName => $propertyValue) {
             $columnMap = $dataMap->getColumnMap($propertyName);
+            if ($columnMap === null) {
+                continue;
+            }
             $propertyMetaData = $classSchema->getProperty($propertyName);
             if ($propertyMetaData['cascade'] === 'remove') {
                 if ($columnMap->getTypeOfRelation() === \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_MANY) {
@@ -1101,6 +1104,10 @@ class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
                 } elseif ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
                     $this->removeEntity($propertyValue);
                 }
+            } elseif ($dataMap->getDeletedFlagColumnName() === null
+                && $columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY
+            ) {
+                $this->deleteAllRelationsFromRelationtable($object, $propertyName);
             }
         }
     }
-- 
GitLab