mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Use sql statements for transferData method and fix updateReferences method
This commit is contained in:
parent
bf14c92567
commit
2185791665
@ -11,8 +11,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Service;
|
||||
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\DBAL\Exception;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
|
||||
class ThirdpartyMergeService
|
||||
{
|
||||
@ -26,96 +30,107 @@ class ThirdpartyMergeService
|
||||
// Update linked entities
|
||||
$this->updateReferences($toKeep, $toDelete);
|
||||
|
||||
// Safely remove the old ThirdParty
|
||||
$this->em->remove($toDelete);
|
||||
$this->em->getConnection()->executeQuery('REFRESH MATERIALIZED VIEW view_chill_main_address_geographical_unit');
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function transferData(ThirdParty $toKeep, ThirdParty $toDelete): void
|
||||
{
|
||||
$excludedProperties = ['id', 'createdAt'];
|
||||
$reflection = new \ReflectionClass(ThirdParty::class);
|
||||
$conn = $this->em->getConnection();
|
||||
|
||||
foreach ($reflection->getProperties() as $property) {
|
||||
if (in_array($property->getName(), $excludedProperties, true)) {
|
||||
continue;
|
||||
$columns = ['profession', 'firstname', 'name', 'email', 'telephone', 'comment', 'kind', 'contact_data_anonymous', 'types', 'active', 'name_company'];
|
||||
|
||||
$conn->beginTransaction();
|
||||
$metadata = $this->em->getClassMetadata(ThirdParty::class);
|
||||
|
||||
foreach ($columns as $column) {
|
||||
$columnType = $metadata->getTypeOfField($column);
|
||||
|
||||
if ('string' === $columnType || 'text' === $columnType) {
|
||||
$sqlUpdate = "
|
||||
UPDATE chill_3party.third_party
|
||||
SET {$column} = COALESCE((SELECT {$column} FROM chill_3party.third_party WHERE id = :toDelete), {$column})
|
||||
WHERE id = :toKeep AND ({$column} IS NULL OR {$column} = '')";
|
||||
} else {
|
||||
$sqlUpdate = "
|
||||
UPDATE chill_3party.third_party
|
||||
SET {$column} = COALESCE((SELECT {$column} FROM chill_3party.third_party WHERE id = :toDelete), {$column})
|
||||
WHERE id = :toKeep AND {$column} IS NULL";
|
||||
}
|
||||
|
||||
$toKeepValue = $property->getValue($toKeep);
|
||||
$toDeleteValue = $property->getValue($toDelete);
|
||||
|
||||
if (null === $toKeepValue && null !== $toDeleteValue) {
|
||||
$property->setValue($toKeep, $toDeleteValue);
|
||||
// Execute the query
|
||||
$conn->executeQuery($sqlUpdate, [
|
||||
'toDelete' => $toDelete->getId(),
|
||||
'toKeep' => $toKeep->getId(),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($toKeepValue instanceof \Doctrine\Common\Collections\Collection
|
||||
&& $toDeleteValue instanceof \Doctrine\Common\Collections\Collection) {
|
||||
foreach ($toDeleteValue as $item) {
|
||||
if (!$toKeepValue->contains($item)) {
|
||||
$toKeepValue->add($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$conn->commit();
|
||||
}
|
||||
|
||||
private function updateReferences(ThirdParty $toKeep, ThirdParty $toDelete): void
|
||||
{
|
||||
$allMeta = $this->em->getMetadataFactory()->getAllMetadata();
|
||||
$conn = $this->em->getConnection();
|
||||
|
||||
foreach ($allMeta as $meta) {
|
||||
if ($meta->isMappedSuperclass) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tableName = $meta->getTableName();
|
||||
|
||||
foreach ($meta->getAssociationMappings() as $assoc) {
|
||||
if (ThirdParty::class !== $assoc['targetEntity']) {
|
||||
continue; // Skip unrelated associations
|
||||
continue;
|
||||
}
|
||||
|
||||
$entityClass = $meta->getName();
|
||||
$associationField = $assoc['fieldName'];
|
||||
if ($assoc['type'] & ClassMetadata::TO_ONE) {
|
||||
|
||||
if ($assoc['type'] & \Doctrine\ORM\Mapping\ClassMetadata::TO_ONE) {
|
||||
// Handle ManyToOne or OneToOne
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$qb->update($entityClass, 'e')
|
||||
->set("e.{$associationField}", ':toKeep')
|
||||
->where("e.{$associationField} = :toDelete")
|
||||
->setParameter('toKeep', $toKeep)
|
||||
->setParameter('toDelete', $toDelete)
|
||||
->getQuery()
|
||||
->execute();
|
||||
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
|
||||
|
||||
if (ThirdParty::class === $assoc['sourceEntity']) {
|
||||
$sql = "UPDATE chill_3party.{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete";
|
||||
} else {
|
||||
$sql = "UPDATE {$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete";
|
||||
}
|
||||
|
||||
if ($assoc['type'] & \Doctrine\ORM\Mapping\ClassMetadata::TO_MANY) {
|
||||
// Handle ManyToMany or OneToMany (inverse side)
|
||||
$repo = $this->em->getRepository($entityClass);
|
||||
$linkedEntities = $repo->createQueryBuilder('e')
|
||||
->join("e.{$associationField}", 't')
|
||||
->where('t = :toDelete')
|
||||
->setParameter('toDelete', $toDelete)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
$conn->executeStatement($sql, [
|
||||
'toKeep' => $toKeep->getId(),
|
||||
'toDelete' => $toDelete->getId(),
|
||||
]);
|
||||
|
||||
foreach ($linkedEntities as $entity) {
|
||||
$getter = 'get'.ucfirst($associationField);
|
||||
$setter = 'set'.ucfirst($associationField);
|
||||
$adder = 'add'.ucfirst(rtrim($associationField, 's'));
|
||||
$remover = 'remove'.ucfirst(rtrim($associationField, 's'));
|
||||
|
||||
if (method_exists($entity, $getter) && method_exists($entity, $setter)) {
|
||||
// For OneToMany owning side
|
||||
$collection = $entity->{$getter}();
|
||||
if ($collection->contains($toDelete)) {
|
||||
$collection->removeElement($toDelete);
|
||||
if (!$collection->contains($toKeep)) {
|
||||
$collection->add($toKeep);
|
||||
if ('parent' === $assoc['fieldName'] && ThirdParty::class === $assoc['targetEntity']) {
|
||||
// Refresh $toKeep to sync its children collection
|
||||
$this->em->refresh($toKeep);
|
||||
}
|
||||
}
|
||||
} elseif (method_exists($entity, $adder) && method_exists($entity, $remover)) {
|
||||
// For ManyToMany
|
||||
$entity->{$remover}($toDelete);
|
||||
$entity->{$adder}($toKeep);
|
||||
}
|
||||
|
||||
$this->em->persist($entity);
|
||||
if ($assoc['type'] & ClassMetadata::TO_MANY) {
|
||||
if (!isset($assoc['joinTable'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$joinTable = $assoc['joinTable']['name'];
|
||||
|
||||
if ($assoc['isOwningSide']) {
|
||||
$joinColumn = $assoc['joinTable']['inverseJoinColumns'][0]['name'];
|
||||
} else {
|
||||
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
|
||||
}
|
||||
|
||||
$sql = "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete";
|
||||
|
||||
$conn->executeStatement($sql, [
|
||||
'toKeep' => $toKeep->getId(),
|
||||
'toDelete' => $toDelete->getId(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user