Fix updating of manyToMany relationships

This commit is contained in:
Julie Lenaerts 2025-03-27 13:34:16 +01:00
parent 5e6833975b
commit 4cea678e93
2 changed files with 12 additions and 2 deletions

View File

@ -45,7 +45,6 @@ class AccompanyingPeriodWorkDuplicateController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
dump($form->get('acpw')->getData());
$acpw2 = $this->accompanyingPeriodWorkRepository->find($form->get('acpw')->getData());
$direction = $form->get('direction')->getData();

View File

@ -173,10 +173,21 @@ class AccompanyingPeriodWorkMergeService
];
} elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) {
if ($assoc['isOwningSide']) {
dump($assoc);
$joinTable = $assoc['joinTable']['name'];
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
$relationColumn = $assoc['joinTable']['inverseJoinColumns'][0]['name'];
$queries[] = [
'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete AND NOT EXISTS (SELECT 1 FROM {$joinTable} WHERE {$joinColumn} = :toKeep)",
'sql' => "
UPDATE {$joinTable} SET {$joinColumn} = :toKeep
WHERE {$joinColumn} = :toDelete
AND NOT EXISTS (
SELECT 1
FROM {$joinTable} AS t2
WHERE t2.{$joinColumn} = :toKeep
AND t2.{$relationColumn} = {$joinTable}.{$relationColumn}
)
",
'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()],
];