mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Resolve manyToMany cases
This commit is contained in:
parent
5999c73c98
commit
13b1d20ade
@ -21,7 +21,7 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
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')]
|
||||
public function findManuallyDuplicateAction(mixed $thirdparty_id, Request $request)
|
||||
@ -41,10 +41,6 @@ class ThirdpartyDuplicateController extends AbstractController
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$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();
|
||||
|
||||
if ('starting' === $direction) {
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?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;
|
||||
|
||||
use Chill\ThirdPartyBundle\Form\Type\PickThirdpartyDynamicType;
|
||||
@ -28,5 +37,4 @@ class ThirdpartyFindDuplicateType extends AbstractType
|
||||
{
|
||||
return 'chill_thirdpartybundle_thirdparty_find_manually_duplicate';
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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()],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?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;
|
||||
|
||||
use Chill\ActivityBundle\Entity\Activity;
|
||||
@ -10,6 +19,11 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||
use libphonenumber\PhoneNumber;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
{
|
||||
private $mergeManager;
|
||||
@ -33,7 +47,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
try {
|
||||
// Rollback the transaction after each test to ensure no data is persisted
|
||||
$this->connection->rollBack();
|
||||
} catch (\Exception $e) {
|
||||
} catch (\Exception) {
|
||||
$this->connection->close();
|
||||
}
|
||||
|
||||
@ -47,7 +61,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
$toKeep->setName('Thirdparty ToKeep');
|
||||
$toKeep->setEmail('keep@example.com');
|
||||
|
||||
$toDelete = new Thirdparty();
|
||||
$toDelete = new ThirdParty();
|
||||
$toDelete->setName('Thirdparty ToDelete'); // This should be ignored
|
||||
$toDelete->setTelephone(new PhoneNumber('123456789'));
|
||||
|
||||
@ -69,7 +83,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
$this->em->clear();
|
||||
|
||||
// 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->assertEquals('Primary Name', $mergedThirdparty->getName(), 'Name should remain unchanged');
|
||||
$this->assertEquals('keep@example.com', $mergedThirdparty->getEmail(), 'Email should remain unchanged');
|
||||
@ -82,7 +96,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
'Activity should be linked to the merged Thirdparty'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$updatedActivity->getThirdParties()->exists(fn($key, $tp) => $tp->getId() === $toDelete->getId()),
|
||||
$updatedActivity->getThirdParties()->exists(fn ($key, $tp) => $tp->getId() === $toDelete->getId()),
|
||||
'Activity should no longer reference the deleted Thirdparty'
|
||||
);
|
||||
|
||||
@ -94,7 +108,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
);
|
||||
|
||||
// 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');
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user