From 1b6e5983ee38ee61c595df81dbdab05928cb4233 Mon Sep 17 00:00:00 2001 From: Claus Due Date: Mon, 26 Feb 2018 16:09:48 +0100 Subject: [PATCH] Forcibly remove all *:n relations before import Unfortunately, due to the way Extbase persistence works with references that get removed, deletion of the M:N record does not always occur. This means that there can be leftover relations if an entity was previously imported with more/different relations and then imported again with new/updated relations. Most prominently a concern with sys_file_reference which masquerades as M:N but is not really an M:N with cascading flushes. --- Classes/Mapping/AbstractMapping.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Classes/Mapping/AbstractMapping.php b/Classes/Mapping/AbstractMapping.php index 54d80d9..7745caf 100644 --- a/Classes/Mapping/AbstractMapping.php +++ b/Classes/Mapping/AbstractMapping.php @@ -139,6 +139,13 @@ protected function mapPropertyValueToObject($propertyName, $propertyValue, $obje $childType = substr($targetType, strpos($targetType, '<') + 1, -1); $childType = trim($childType, '\\'); $objectStorage = ObjectAccess::getProperty($object, $propertyName) ?? new ObjectStorage(); + // Step one is to detach all currently related objects. Please note that $objectStorage->removeAll($objectStorage) + // does not work due to array pointer reset issues with Iterators. The only functioning way is to iterate and + // detach all, one by one, as below. + foreach ($objectStorage->toArray() as $item) { + $objectStorage->detach($item); + } + foreach ((array) $propertyValue as $identifier) { if (!$identifier) { continue;