Resolve manyToMany cases

This commit is contained in:
Julie Lenaerts 2025-02-19 12:35:25 +01:00
parent 5999c73c98
commit 13b1d20ade
4 changed files with 57 additions and 23 deletions

View File

@ -21,7 +21,7 @@ use Symfony\Component\Routing\Annotation\Route;
class ThirdpartyDuplicateController extends AbstractController class ThirdpartyDuplicateController extends AbstractController
{ {
public function __construct(private ThirdPartyRepository $thirdPartyRepository, private ThirdpartyMergeService $thirdPartyMergeService) {} public function __construct(private readonly ThirdPartyRepository $thirdPartyRepository, private readonly ThirdpartyMergeService $thirdPartyMergeService) {}
#[Route(path: '/{_locale}/3party/{thirdparty_id}/find-manually', name: 'chill_thirdparty_find_duplicate')] #[Route(path: '/{_locale}/3party/{thirdparty_id}/find-manually', name: 'chill_thirdparty_find_duplicate')]
public function findManuallyDuplicateAction(mixed $thirdparty_id, Request $request) public function findManuallyDuplicateAction(mixed $thirdparty_id, Request $request)
@ -41,10 +41,6 @@ class ThirdpartyDuplicateController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$thirdparty2 = $form->get('thirdparty')->getData(); $thirdparty2 = $form->get('thirdparty')->getData();
if (null === $thirdparty2) {
throw $this->createNotFoundException("Thirdparty with id {$thirdparty2->getId}() not".' found on this server');
}
$direction = $form->get('direction')->getData(); $direction = $form->get('direction')->getData();
if ('starting' === $direction) { if ('starting' === $direction) {

View File

@ -1,5 +1,14 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\ThirdPartyBundle\Form; namespace Chill\ThirdPartyBundle\Form;
use Chill\ThirdPartyBundle\Form\Type\PickThirdpartyDynamicType; use Chill\ThirdPartyBundle\Form\Type\PickThirdpartyDynamicType;
@ -28,5 +37,4 @@ class ThirdpartyFindDuplicateType extends AbstractType
{ {
return 'chill_thirdpartybundle_thirdparty_find_manually_duplicate'; return 'chill_thirdpartybundle_thirdparty_find_manually_duplicate';
} }
} }

View File

@ -63,7 +63,7 @@ class ThirdpartyMergeService
UPDATE chill_3party.third_party UPDATE chill_3party.third_party
SET {$column} = COALESCE((SELECT {$column} FROM chill_3party.third_party WHERE id = :toDelete), {$column}) SET {$column} = COALESCE((SELECT {$column} FROM chill_3party.third_party WHERE id = :toDelete), {$column})
WHERE id = :toKeep AND {$condition}", WHERE id = :toKeep AND {$condition}",
'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()] 'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()],
]; ];
} }
@ -86,19 +86,34 @@ class ThirdpartyMergeService
continue; continue;
} }
if ($assoc['type'] & ClassMetadata::TO_ONE) { if (ClassMetadata::TO_ONE === $assoc['type']) {
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']); $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;
} }
if ($assoc['type'] & ClassMetadata::TO_MANY && isset($assoc['joinTable'])) { $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()],
];
}
if (ClassMetadata::TO_MANY === $assoc['type'] && isset($assoc['joinTable'])) {
$joinTable = $assoc['joinTable']['name']; $joinTable = $assoc['joinTable']['name'];
$joinColumn = ThirdParty::class === $assoc['targetEntity'] $joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
? $assoc['joinTable']['inverseJoinColumns'][0]['name'] if ('thirdparty_id' === $joinColumn) {
: $assoc['joinTable']['joinColumns'][0]['name']; $queries[] = [
//TODO problem remaining for ManyToMany relations when UPDATE triggers a UNIQUE CONSTRAINT...in the case of Thirdparty -> thirdparty_category and thirdparty_center 'sql' => "DELETE FROM {$joinTable} WHERE {$joinColumn} = :toDelete",
'params' => ['toDelete' => $toDelete->getId()],
];
} else {
$queries[] = [ $queries[] = [
'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", 'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
@ -106,6 +121,7 @@ class ThirdpartyMergeService
} }
} }
} }
}
return $queries; return $queries;
} }

View File

@ -1,5 +1,14 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\ThirdPartyBundle\Tests\Service; namespace Chill\ThirdPartyBundle\Tests\Service;
use Chill\ActivityBundle\Entity\Activity; use Chill\ActivityBundle\Entity\Activity;
@ -10,6 +19,11 @@ use Doctrine\ORM\EntityManagerInterface;
use libphonenumber\PhoneNumber; use libphonenumber\PhoneNumber;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
*
* @coversNothing
*/
class ThirdpartyMergeServiceTest extends KernelTestCase class ThirdpartyMergeServiceTest extends KernelTestCase
{ {
private $mergeManager; private $mergeManager;
@ -33,7 +47,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
try { try {
// Rollback the transaction after each test to ensure no data is persisted // Rollback the transaction after each test to ensure no data is persisted
$this->connection->rollBack(); $this->connection->rollBack();
} catch (\Exception $e) { } catch (\Exception) {
$this->connection->close(); $this->connection->close();
} }
@ -47,7 +61,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
$toKeep->setName('Thirdparty ToKeep'); $toKeep->setName('Thirdparty ToKeep');
$toKeep->setEmail('keep@example.com'); $toKeep->setEmail('keep@example.com');
$toDelete = new Thirdparty(); $toDelete = new ThirdParty();
$toDelete->setName('Thirdparty ToDelete'); // This should be ignored $toDelete->setName('Thirdparty ToDelete'); // This should be ignored
$toDelete->setTelephone(new PhoneNumber('123456789')); $toDelete->setTelephone(new PhoneNumber('123456789'));
@ -69,7 +83,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
$this->em->clear(); $this->em->clear();
// Verify data was merged correctly // Verify data was merged correctly
$mergedThirdparty = $this->em->getRepository(Thirdparty::class)->find($toKeep->getId()); $mergedThirdparty = $this->em->getRepository(ThirdParty::class)->find($toKeep->getId());
$this->assertNotNull($mergedThirdparty); $this->assertNotNull($mergedThirdparty);
$this->assertEquals('Primary Name', $mergedThirdparty->getName(), 'Name should remain unchanged'); $this->assertEquals('Primary Name', $mergedThirdparty->getName(), 'Name should remain unchanged');
$this->assertEquals('keep@example.com', $mergedThirdparty->getEmail(), 'Email should remain unchanged'); $this->assertEquals('keep@example.com', $mergedThirdparty->getEmail(), 'Email should remain unchanged');
@ -94,7 +108,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
); );
// Ensure the 'toDelete' entity is removed // Ensure the 'toDelete' entity is removed
$deletedThirdparty = $this->em->getRepository(Thirdparty::class)->find($toDelete->getId()); $deletedThirdparty = $this->em->getRepository(ThirdParty::class)->find($toDelete->getId());
$this->assertNull($deletedThirdparty, 'The deleted Thirdparty should no longer exist in the database'); $this->assertNull($deletedThirdparty, 'The deleted Thirdparty should no longer exist in the database');
} }
} }