Fix merge service and test

This commit is contained in:
Julie Lenaerts 2025-04-01 16:15:34 +02:00
parent 95972399a1
commit b3bf405c5b
2 changed files with 22 additions and 15 deletions

View File

@ -60,18 +60,19 @@ class ThirdpartyMergeService
if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) { if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) {
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']); $joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) { $suffix = (ThirdParty::class === $assoc['sourceEntity']) ? 'chill_3party.' : '';
continue;
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' === $joinColumn) {
$queries[] = [
'sql' => "UPDATE {$suffix}{$tableName} SET parent_id = (SELECT parent_id FROM chill_3party.third_party WHERE id = :toDelete) WHERE id = :toKeep",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
];
} else {
$queries[] = [
'sql' => "UPDATE {$suffix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
];
} }
$schemaPrefix = (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' === $joinColumn)
? 'chill_3party.'
: '';
$queries[] = [
'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
];
} elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) { } elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) {
$joinTable = $assoc['joinTable']['name']; $joinTable = $assoc['joinTable']['name'];
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name']; $joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];

View File

@ -39,35 +39,41 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
{ {
// Create ThirdParty entities // Create ThirdParty entities
$toKeep = new ThirdParty(); $toKeep = new ThirdParty();
$toKeep->setName('Thirdparty to keep');
$this->em->persist($toKeep); $this->em->persist($toKeep);
$toDelete = new ThirdParty(); $toDelete = new ThirdParty();
$toDelete->setName('Thirdparty to delete');
$this->em->persist($toDelete); $this->em->persist($toDelete);
// Create a related entity with TO_ONE relation (thirdparty parent) // Create a related entity with TO_ONE relation (thirdparty parent)
$relatedToOneEntity = new ThirdParty(); $relatedToOneEntity = new ThirdParty();
$relatedToOneEntity->setName('RelatedToOne thirdparty');
$this->em->persist($relatedToOneEntity); $this->em->persist($relatedToOneEntity);
$toDelete->setParent($relatedToOneEntity); $toDelete->setParent($relatedToOneEntity);
$this->em->persist($toDelete); $this->em->persist($toDelete);
// Create a related entity with TO_MANY relation (thirdparty category) // Create a related entity with TO_MANY relation (thirdparty category)
$relatedManyEntity = new ThirdPartyCategory(); $thirdpartyCategory = new ThirdPartyCategory();
$this->em->persist($relatedManyEntity); $thirdpartyCategory->setName(['fr' => 'Thirdparty category']);
$toDelete->addCategory($relatedManyEntity); $this->em->persist($thirdpartyCategory);
$toDelete->addCategory($thirdpartyCategory);
$this->em->persist($toDelete); $this->em->persist($toDelete);
$this->em->flush(); $this->em->flush();
// Run merge // Run merge
$this->service->merge($toKeep, $toDelete); $this->service->merge($toKeep, $toDelete);
$this->em->refresh($toKeep);
// Check that references were updated // Check that references were updated
$this->assertEquals($toKeep->getParent()->getId(), $relatedToOneEntity->getId(), 'The parent thirdparty was succesfully merged'); $this->assertEquals($toKeep->getParent()->getId(), $relatedToOneEntity->getId(), 'The parent thirdparty was succesfully merged');
$updatedRelatedManyEntity = $this->em->find(ThirdPartyCategory::class, $relatedManyEntity->getId()); $updatedRelatedManyEntity = $this->em->find(ThirdPartyCategory::class, $thirdpartyCategory->getId());
$this->assertContains($updatedRelatedManyEntity, $toKeep->getCategories(), 'The thirdparty category was found in the toKeep entity'); $this->assertContains($updatedRelatedManyEntity, $toKeep->getCategories(), 'The thirdparty category was found in the toKeep entity');
// Check that toDelete was removed // Check that toDelete was removed
$this->em->clear();
$deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId()); $deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId());
$this->assertNull($deletedThirdParty); $this->assertNull($deletedThirdParty);
} }