mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
131 lines
4.3 KiB
PHP
131 lines
4.3 KiB
PHP
<?php
|
|
/*
|
|
* Copyright (C) 20169 Champs-Libres <info@champs-libres.coop>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
namespace Chill\PersonBundle\Actions\Remove;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
/**
|
|
* Move all person to a new one, and delete the old record.
|
|
*
|
|
*/
|
|
class PersonMove
|
|
{
|
|
/**
|
|
*
|
|
* @var EntityManagerInterface
|
|
*/
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
}
|
|
|
|
public function getSQL(Person $from, Person $to)
|
|
{
|
|
$sqls = [];
|
|
|
|
foreach ($this->em->getMetadataFactory()->getAllMetadata() as $metadata) {
|
|
if ($metadata->isMappedSuperclass) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($metadata->getAssociationMappings() as $field => $mapping) {
|
|
if ($mapping['targetEntity'] === Person::class) {
|
|
if (\in_array($metadata->getName(), $this->deleteEntities())) {
|
|
$sqls[] = $this->createDeleteSQL($metadata, $from, $field);
|
|
} else {
|
|
$sqls[] = $this->createMoveSQL($metadata, $from, $to, $field);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$personMetadata = $this->em->getClassMetadata(Person::class);
|
|
$sqls[] = sprintf("DELETE FROM %s WHERE id = %d",
|
|
$this->getTableName($personMetadata),
|
|
$from->getId());
|
|
|
|
return $sqls ?? [];
|
|
}
|
|
|
|
protected function createMoveSQL(ClassMetadata $metadata, Person $from, Person $to, $field): string
|
|
{
|
|
$mapping = $metadata->getAssociationMapping($field);
|
|
|
|
// Set part of the query, aka <here> in "UPDATE table SET <here> "
|
|
$sets = [];
|
|
foreach ($mapping["joinColumns"] as $columns) {
|
|
$sets[] = sprintf("%s = %d", $columns["name"], $to->getId());
|
|
}
|
|
|
|
$conditions = [];
|
|
foreach ($mapping["joinColumns"] as $columns) {
|
|
$conditions[] = sprintf("%s = %d", $columns["name"], $from->getId());
|
|
}
|
|
|
|
return \sprintf("UPDATE %s SET %s WHERE %s",
|
|
$this->getTableName($metadata),
|
|
\implode(" ", $sets),
|
|
\implode(" AND ", $conditions)
|
|
);
|
|
}
|
|
|
|
protected function createDeleteSQL(ClassMetadata $metadata, Person $from, $field): string
|
|
{
|
|
$mapping = $metadata->getAssociationMapping($field);
|
|
|
|
$conditions = [];
|
|
foreach ($mapping["joinColumns"] as $columns) {
|
|
$conditions[] = sprintf("%s = %d", $columns["name"], $from->getId());
|
|
}
|
|
|
|
return \sprintf("DELETE FROM %s WHERE %s",
|
|
$this->getTableName($metadata),
|
|
\implode(" AND ", $conditions)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* return an array of classes where entities should be deleted
|
|
* instead of moved
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function deleteEntities(): array
|
|
{
|
|
return [
|
|
AccompanyingPeriod::class
|
|
];
|
|
}
|
|
|
|
/**
|
|
* get the full table name with schema if it does exists
|
|
*/
|
|
private function getTableName(ClassMetadata $metadata): string
|
|
{
|
|
return empty($metadata->getSchemaName()) ?
|
|
$metadata->getTableName() :
|
|
$metadata->getSchemaName().".".$metadata->getTableName();
|
|
}
|
|
|
|
}
|