diff --git a/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php b/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php index 22956c8d0..650065cc0 100644 --- a/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php +++ b/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php @@ -60,18 +60,19 @@ class ThirdpartyMergeService if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) { $joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']); - if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) { - continue; + $suffix = (ThirdParty::class === $assoc['sourceEntity']) ? 'chill_3party.' : ''; + + 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'])) { $joinTable = $assoc['joinTable']['name']; $joinColumn = $assoc['joinTable']['joinColumns'][0]['name']; diff --git a/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php b/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php index a1d694c43..f25da94e9 100644 --- a/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php +++ b/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php @@ -39,35 +39,41 @@ class ThirdpartyMergeServiceTest extends KernelTestCase { // Create ThirdParty entities $toKeep = new ThirdParty(); + $toKeep->setName('Thirdparty to keep'); $this->em->persist($toKeep); $toDelete = new ThirdParty(); + $toDelete->setName('Thirdparty to delete'); $this->em->persist($toDelete); // Create a related entity with TO_ONE relation (thirdparty parent) $relatedToOneEntity = new ThirdParty(); + $relatedToOneEntity->setName('RelatedToOne thirdparty'); $this->em->persist($relatedToOneEntity); $toDelete->setParent($relatedToOneEntity); $this->em->persist($toDelete); // Create a related entity with TO_MANY relation (thirdparty category) - $relatedManyEntity = new ThirdPartyCategory(); - $this->em->persist($relatedManyEntity); - $toDelete->addCategory($relatedManyEntity); + $thirdpartyCategory = new ThirdPartyCategory(); + $thirdpartyCategory->setName(['fr' => 'Thirdparty category']); + $this->em->persist($thirdpartyCategory); + $toDelete->addCategory($thirdpartyCategory); $this->em->persist($toDelete); $this->em->flush(); // Run merge $this->service->merge($toKeep, $toDelete); + $this->em->refresh($toKeep); // Check that references were updated $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'); // Check that toDelete was removed + $this->em->clear(); $deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId()); $this->assertNull($deletedThirdParty); }