diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php index cb18bd4cd..53654f397 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -1,5 +1,14 @@ getId(), $from->getId(), $to->getId()); - $deleteSql = sprintf(<<getId()); diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index 220adcfc1..c4307259f 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -1,19 +1,25 @@ getId(), $from->getId(), $to->getId()); + SQL, $to->getId(), $from->getId(), $to->getId()); - $deleteSql = sprintf(<<getId()); - return [$sqlInsert, $deleteSql]; + return [$sqlInsert, $deleteSql]; } } diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php index 3454fce24..82ca0592c 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php @@ -1,5 +1,14 @@ getId(), $from->getId(), $to->getId(), $from->getId(), $from->getId(), $to->getId()); - $deleteSql = sprintf(<<getId(), $from->getId()); diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index c3ecd8ec1..067727078 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -37,7 +37,7 @@ class PersonMove { public function __construct( private EntityManagerInterface $em, - private PersonMoveManager $personMoveManager, + private PersonMoveManager $personMoveManager, private EventDispatcherInterface $eventDispatcher ) { } @@ -78,9 +78,9 @@ class PersonMove foreach ($metadata->getAssociationMappings() as $field => $mapping) { - if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) { - $sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to)); - continue; + if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) { + $sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to)); + continue; } if (in_array($mapping['sourceEntity'], $this->getIgnoredEntities(), true)) { @@ -148,32 +148,28 @@ class PersonMove $tableName = ''; if (array_key_exists('joinTable', $mapping)) { - // there is a join_table: we have to find conflict + // there is a join_table: we have to find conflict $tableName = (null !== ($mapping['joinTable']['schema'] ?? null) ? $mapping['joinTable']['schema'] . '.' : '') . $mapping['joinTable']['name']; $sqlInsert = sprintf( - "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING", - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id - $to->getId(), - $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $from->getId() - ); + "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $to->getId(), + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + ); $deleteSql = sprintf( - "DELETE FROM %s WHERE %s = %d", - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $from->getId() - ); - - return [ - $sqlInsert, $deleteSql - ]; + "DELETE FROM %s WHERE %s = %d", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + ); foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); @@ -183,7 +179,12 @@ class PersonMove $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); } - } elseif (array_key_exists('joinColumns', $mapping)) { + return [ + $sqlInsert, $deleteSql + ]; + + } + if (array_key_exists('joinColumns', $mapping)) { $tableName = $this->getTableName($metadata); foreach ($mapping['joinColumns'] as $columns) { $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php index a7bd1188a..99d22c3bb 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php @@ -1,5 +1,14 @@ */ private iterable $handlers, - ) - { + ) { } /** @@ -20,7 +28,7 @@ class PersonMoveManager * @param string $field * @return bool */ - public function hasHandler(string $className, string $field): bool + public function hasHandler(string $className, string $field): bool { foreach ($this->handlers as $handler) { if ($handler->supports($className, $field)) { @@ -28,14 +36,14 @@ class PersonMoveManager } } - return false; + return false; } /** * @param class-string $className * @return array */ - public function getSqls(string $className, string $field, Person $from, Person $to): array + public function getSqls(string $className, string $field, Person $from, Person $to): array { foreach ($this->handlers as $handler) { if ($handler->supports($className, $field)) { diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php index 9958f59df..c1b47127d 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php @@ -1,5 +1,14 @@ */ diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 14268160e..19820ae1e 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -1,5 +1,14 @@ em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { - foreach ($sqls as $sql) { - $conn->executeStatement($sql); - } + foreach ($sqls as $sql) { + $conn->executeStatement($sql); + } }); $personsByIdOfA = $this->em->createQuery("SELECT p FROM " . Person::class . " p WHERE p.id = :id")