From f25c2859ec864c0528ff5d2427ef0a1f2f2279e6 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 6 Nov 2025 12:43:06 +0100 Subject: [PATCH] Fix the fusion of thirdparty properties that are located in another schema than public for TO_ONE relations + add extra loop for MANY_TO_MANY relations where thirdparty is the source instead of the target --- .../unreleased/Fixed-20251106-123641.yaml | 6 ++++ .../Service/ThirdpartyMergeService.php | 31 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 .changes/unreleased/Fixed-20251106-123641.yaml diff --git a/.changes/unreleased/Fixed-20251106-123641.yaml b/.changes/unreleased/Fixed-20251106-123641.yaml new file mode 100644 index 000000000..a65a6ea8b --- /dev/null +++ b/.changes/unreleased/Fixed-20251106-123641.yaml @@ -0,0 +1,6 @@ +kind: Fixed +body: Fix the fusion of thirdparty properties that are located in another schema than public for TO_ONE relations + add extra loop for MANY_TO_MANY relations where thirdparty is the source instead of the target +time: 2025-11-06T12:36:41.555991969+01:00 +custom: + Issue: "457" + SchemaChange: No schema change diff --git a/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php b/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php index 4eecb09b4..daea6627f 100644 --- a/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php +++ b/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php @@ -69,10 +69,11 @@ readonly class ThirdpartyMergeService if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) { $joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']); - $suffix = (ThirdParty::class === $assoc['sourceEntity']) ? 'chill_3party.' : ''; + $schema = $meta->getSchemaName(); + $prefix = null !== $schema && '' !== $schema ? $schema.'.' : ''; $queries[] = [ - 'sql' => "UPDATE {$suffix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", + 'sql' => "UPDATE {$prefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], ]; } elseif (ClassMetadata::MANY_TO_MANY === $assoc['type'] && isset($assoc['joinTable'])) { @@ -85,13 +86,33 @@ readonly class ThirdpartyMergeService ]; $queries[] = [ - 'sql' => "DELETE FROM {$joinTable} WHERE {$joinColumn} = :toDelete", + 'sql' => "DELETE FROM {$prefix}{$joinTable} WHERE {$joinColumn} = :toDelete", 'params' => ['toDelete' => $toDelete->getId()], ]; } } } + // Also handle many-to-many where ThirdParty is the source + $thirdPartyMeta = $this->em->getClassMetadata(ThirdParty::class); + foreach ($thirdPartyMeta->getAssociationMappings() as $assoc) { + if (ClassMetadata::MANY_TO_MANY === $assoc['type'] && isset($assoc['joinTable'])) { + $joinTable = $assoc['joinTable']['name']; + $prefix = null !== ($assoc['joinTable']['schema'] ?? null) ? $assoc['joinTable']['schema'].'.' : ''; + $joinColumn = $assoc['joinTable']['joinColumns'][0]['name']; // Note: joinColumns, not inverseJoinColumns + + $queries[] = [ + 'sql' => "UPDATE {$prefix}{$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", + 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], + ]; + + $queries[] = [ + 'sql' => "DELETE FROM {$prefix}{$joinTable} WHERE {$joinColumn} = :toDelete", + 'params' => ['toDelete' => $toDelete->getId()], + ]; + } + } + return $queries; } @@ -102,10 +123,10 @@ readonly class ThirdpartyMergeService 'sql' => 'UPDATE chill_3party.third_party SET parent_id = :toKeep WHERE parent_id = :toDelete', 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], ], - [ + /* [ 'sql' => 'UPDATE chill_3party.thirdparty_category SET thirdparty_id = :toKeep WHERE thirdparty_id = :toDelete AND NOT EXISTS (SELECT 1 FROM chill_3party.thirdparty_category WHERE thirdparty_id = :toKeep)', 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], - ], + ],*/ [ 'sql' => 'DELETE FROM chill_3party.third_party WHERE id = :toDelete', 'params' => ['toDelete' => $toDelete->getId()],