Resolve manyToMany cases

This commit is contained in:
2025-02-19 12:35:25 +01:00
parent 480dcf2b2b
commit 5b1b0f7fb1
4 changed files with 57 additions and 23 deletions

View File

@@ -63,7 +63,7 @@ class ThirdpartyMergeService
UPDATE chill_3party.third_party
SET {$column} = COALESCE((SELECT {$column} FROM chill_3party.third_party WHERE id = :toDelete), {$column})
WHERE id = :toKeep AND {$condition}",
'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()]
'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()],
];
}
@@ -86,24 +86,40 @@ class ThirdpartyMergeService
continue;
}
if ($assoc['type'] & ClassMetadata::TO_ONE) {
if (ClassMetadata::TO_ONE === $assoc['type']) {
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
$sql = "UPDATE {$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete";
$queries[] = ['sql' => $sql, 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()]];
}
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) {
// TODO what with 'address_id' and 'civility_id'? This condition also contains columns like updatedBy_id which we want to ignore...
continue;
}
$schemaPrefix = (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' === $joinColumn)
? 'chill_3party.'
: '';
if ($assoc['type'] & ClassMetadata::TO_MANY && isset($assoc['joinTable'])) {
$joinTable = $assoc['joinTable']['name'];
$joinColumn = ThirdParty::class === $assoc['targetEntity']
? $assoc['joinTable']['inverseJoinColumns'][0]['name']
: $assoc['joinTable']['joinColumns'][0]['name'];
//TODO problem remaining for ManyToMany relations when UPDATE triggers a UNIQUE CONSTRAINT...in the case of Thirdparty -> thirdparty_category and thirdparty_center
$queries[] = [
'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
];
}
if (ClassMetadata::TO_MANY === $assoc['type'] && isset($assoc['joinTable'])) {
$joinTable = $assoc['joinTable']['name'];
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
if ('thirdparty_id' === $joinColumn) {
$queries[] = [
'sql' => "DELETE FROM {$joinTable} WHERE {$joinColumn} = :toDelete",
'params' => ['toDelete' => $toDelete->getId()],
];
} else {
$queries[] = [
'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
];
}
}
}
}