mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Fix merge service and passing of json to vue
This commit is contained in:
parent
e1d308fd97
commit
ae679e6997
@ -11,30 +11,43 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Chill\PersonBundle\Controller;
|
namespace Chill\PersonBundle\Controller;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||||
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||||
use Chill\PersonBundle\Form\FindAccompanyingPeriodWorkType;
|
use Chill\PersonBundle\Form\FindAccompanyingPeriodWorkType;
|
||||||
|
use Chill\PersonBundle\Form\PersonConfimDuplicateType;
|
||||||
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
|
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
|
||||||
|
use Chill\PersonBundle\Service\AccompanyingPeriodWork\AccompanyingPeriodWorkMergeService;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||||
class AccompanyingPeriodWorkDuplicateController extends AbstractController
|
class AccompanyingPeriodWorkDuplicateController extends AbstractController
|
||||||
{
|
{
|
||||||
public function __construct(private readonly AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository, private readonly TranslatorInterface $translator) {}
|
public function __construct(private readonly AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository, private readonly TranslatorInterface $translator, private readonly TranslatableStringHelperInterface $stringHelper, private readonly AccompanyingPeriodWorkMergeService $accompanyingPeriodWorkMergeService) {}
|
||||||
|
|
||||||
#[Route(path: '{_locale}/person/accompanying-period/work/{id}/assign-duplicate', name: 'chill_person_accompanying_period_work_assign_duplicate', methods: ['GET'])]
|
/**
|
||||||
public function assignDuplicate(int $id, Request $request)
|
* @ParamConverter("accompanyingPeriodWork", options={"id": "acpw_id"})
|
||||||
|
*/
|
||||||
|
#[Route(path: '{_locale}/person/accompanying-period/work/{id}/assign-duplicate', name: 'chill_person_accompanying_period_work_assign_duplicate')]
|
||||||
|
public function assignDuplicate(AccompanyingPeriodWork $acpw, Request $request)
|
||||||
{
|
{
|
||||||
$acpw1= $this->accompanyingPeriodWorkRepository->find($id);
|
$accompanyingPeriod = $acpw->getAccompanyingPeriod();
|
||||||
$accompanyingPeriod = $acpw1->getAccompanyingPeriod();
|
|
||||||
|
|
||||||
if (null === $acpw1) {
|
$acpwArray = $this->accompanyingPeriodWorkRepository->findByAccompanyingPeriod($accompanyingPeriod);
|
||||||
throw $this->createNotFoundException("Accompanying period work with id {$id} not".' found on this server');
|
|
||||||
}
|
$acpwArray = array_map(function ($acpw) {
|
||||||
|
return [
|
||||||
|
'id' => $acpw->getId(),
|
||||||
|
'socialAction' => $this->stringHelper->localize($acpw->getSocialAction()->getTitle()),
|
||||||
|
'startDate' => $acpw->getStartDate(),
|
||||||
|
'endDate' => $acpw->getEndDate(),
|
||||||
|
];
|
||||||
|
}, $acpwArray);
|
||||||
|
|
||||||
$this->denyAccessUnlessGranted(
|
$this->denyAccessUnlessGranted(
|
||||||
'CHILL_MAIN_ACCOMPANYING_PERIOD_WORK_UPDATE',
|
'CHILL_MAIN_ACCOMPANYING_PERIOD_WORK_UPDATE',
|
||||||
$acpw1,
|
$acpw,
|
||||||
'You are not allowed to merge this accompanying period work'
|
'You are not allowed to merge this accompanying period work'
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -43,36 +56,82 @@ class AccompanyingPeriodWorkDuplicateController extends AbstractController
|
|||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
$acpw2 = $form->get('person')->getData();
|
$acpw2 = $this->accompanyingPeriodWorkRepository->find($form->get('acpw')->getData());
|
||||||
|
|
||||||
// Validation: Ensure person1 is not the same as person2
|
|
||||||
if ($acpw1->getId() === $acpw2->getId()) {
|
|
||||||
$this->addFlash('error', $this->translator->trans('The entities you are trying to merge cannot be the same'));
|
|
||||||
|
|
||||||
return $this->redirectToRoute('chill_person_accompanying_period_work_show', ['id' => $acpw1->getId()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$direction = $form->get('direction')->getData();
|
$direction = $form->get('direction')->getData();
|
||||||
|
|
||||||
if ('starting' === $direction) {
|
if ('starting' === $direction) {
|
||||||
$params = [
|
$params = [
|
||||||
'acpw1_id' => $acpw1->getId(),
|
'acpw1_id' => $acpw->getId(),
|
||||||
'acpw2_id' => $acpw2->getId(),
|
'acpw2_id' => $acpw2->getId(),
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
$params = [
|
$params = [
|
||||||
'acpw1_id' => $acpw2->getId(),
|
'acpw1_id' => $acpw2->getId(),
|
||||||
'acpw2_id' => $acpw1->getId(),
|
'acpw2_id' => $acpw->getId(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// return $this->redirectToRoute('chill_person_acpw_duplicate_confirm', $params);
|
return $this->redirectToRoute('chill_person_acpw_duplicate_confirm', $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->render('@ChillPerson/AccompanyingPeriodWorkDuplicate/assign_acpw_duplicate.html.twig', [
|
return $this->render('@ChillPerson/AccompanyingPeriodWorkDuplicate/assign_acpw_duplicate.html.twig', [
|
||||||
'accompanyingCourse' => $acpw1->getAccompanyingPeriod(),
|
'accompanyingCourse' => $acpw->getAccompanyingPeriod(),
|
||||||
'acpw' => $acpw1,
|
'acpwArray' => $acpwArray,
|
||||||
|
'acpw' => $acpw,
|
||||||
'form' => $form->createView(),
|
'form' => $form->createView(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ParamConverter("acpw1", options={"id": "acpw1_id"})
|
||||||
|
* @ParamConverter("acpw2", options={"id": "acpw2_id"})
|
||||||
|
*/
|
||||||
|
#[Route(path: '/{_locale}/person/{acpw1_id}/duplicate/{acpw2_id}/confirm', name: 'chill_person_acpw_duplicate_confirm')]
|
||||||
|
public function confirmAction(AccompanyingPeriodWork $acpw1, AccompanyingPeriodWork $acpw2, Request $request)
|
||||||
|
{
|
||||||
|
$accompanyingPeriod = $acpw1->getAccompanyingPeriod();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->validateMerge($acpw1, $acpw2);
|
||||||
|
$form = $this->createForm(PersonConfimDuplicateType::class);
|
||||||
|
|
||||||
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
|
||||||
|
$this->accompanyingPeriodWorkMergeService->merge($acpw1, $acpw2);
|
||||||
|
|
||||||
|
return $this->redirectToRoute('chill_person_accompanying_period_work_show', ['id' => $acpw1->getId()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('@ChillPerson/AccompanyingPeriodWorkDuplicate/confirm.html.twig', [
|
||||||
|
'acpw' => $acpw1,
|
||||||
|
'acpw2' => $acpw2,
|
||||||
|
'accompanyingCourse' => $accompanyingPeriod,
|
||||||
|
'form' => $form->createView(),
|
||||||
|
]);
|
||||||
|
} catch (\InvalidArgumentException $e) {
|
||||||
|
$this->addFlash('error', $this->translator->trans($e->getMessage()));
|
||||||
|
|
||||||
|
return $this->redirectToRoute('chill_person_accompanying_period_work_assign_duplicate', [
|
||||||
|
'id' => $acpw1->getId(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validateMerge(AccompanyingPeriodWork $acpw1, AccompanyingPeriodWork $acpw2): void
|
||||||
|
{
|
||||||
|
$constraints = [
|
||||||
|
[$acpw1 === $acpw2, 'acpw_duplicate.You cannot merge a accompanying period work with itself. Please choose a different one'],
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($constraints as [$condition, $message]) {
|
||||||
|
if ($condition) {
|
||||||
|
throw new \InvalidArgumentException($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import AccompanyingPeriodWorkSelectorModal from "../../vuejs/_components/Accompa
|
|||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
const el = document.getElementById('linked-acpw-selector');
|
const el = document.getElementById('linked-acpw-selector');
|
||||||
if (el) {
|
if (el) {
|
||||||
const acpwList = JSON.parse(el.dataset.acpwList);
|
const acpwList = JSON.parse(el.dataset.acpwArray);
|
||||||
createApp(AccompanyingPeriodWorkSelectorModal, { acpwList }).mount(el);
|
createApp(AccompanyingPeriodWorkSelectorModal, { acpwList }).mount(el);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -42,7 +42,9 @@ import {ISOToDate} from "ChillMainAssets/chill/js/date";
|
|||||||
const props = defineProps<{ acpw: AccompanyingPeriodWork }>();
|
const props = defineProps<{ acpw: AccompanyingPeriodWork }>();
|
||||||
|
|
||||||
const formatDate = (dateObject) => {
|
const formatDate = (dateObject) => {
|
||||||
|
if(dateObject) {
|
||||||
const parsedDate = ISOToDate(dateObject.date);
|
const parsedDate = ISOToDate(dateObject.date);
|
||||||
return new Intl.DateTimeFormat('default', { dateStyle: 'short' }).format(parsedDate);
|
return new Intl.DateTimeFormat('default', { dateStyle: 'short' }).format(parsedDate);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
@ -4,13 +4,15 @@
|
|||||||
|
|
||||||
{% block title %}{{ 'Assign an accompanying period work duplicate' }}{% endblock %}
|
{% block title %}{{ 'Assign an accompanying period work duplicate' }}{% endblock %}
|
||||||
|
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="person-duplicate">
|
<div class="person-duplicate">
|
||||||
|
|
||||||
<h1>{{ 'Assign an accompanying period work duplicate'|trans }}</h1>
|
<h1>{{ 'acpw_duplicate.Assign duplicate'|trans }}</h1>
|
||||||
|
|
||||||
{{ form_start(form) }}
|
{{ form_start(form) }}
|
||||||
|
{%- if form.acpw is defined -%}
|
||||||
|
{{ form_row(form.acpw) }}
|
||||||
|
<div id="linked-acpw-selector" data-acpw-array='{{ acpwArray|json_encode }}'></div>
|
||||||
|
{% endif %}
|
||||||
{{ form_rest(form) }}
|
{{ form_rest(form) }}
|
||||||
|
|
||||||
<ul class="record_actions sticky-form-buttons">
|
<ul class="record_actions sticky-form-buttons">
|
||||||
@ -30,9 +32,9 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block js %}
|
{% block js %}
|
||||||
{{ encore_entry_script_tags('mod_pickentity_type') }}
|
{{ encore_entry_script_tags('mod_duplicate_selector') }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block css %}
|
{% block css %}
|
||||||
{{ encore_entry_link_tags('mod_pickentity_type') }}
|
{{ encore_entry_link_tags('mod_duplicate_selector') }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -12,6 +12,7 @@ declare(strict_types=1);
|
|||||||
namespace Chill\PersonBundle\Service\AccompanyingPeriodWork;
|
namespace Chill\PersonBundle\Service\AccompanyingPeriodWork;
|
||||||
|
|
||||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||||
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkReferrerHistory;
|
||||||
use Doctrine\DBAL\Exception;
|
use Doctrine\DBAL\Exception;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||||
@ -24,57 +25,51 @@ class AccompanyingPeriodWorkMergeService
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function merge(AccompanyingPeriodWork $toKeep, AccompanyingPeriodWork $toDelete): void
|
public function merge(AccompanyingPeriodWork $toKeep, AccompanyingPeriodWork $toDelete): void
|
||||||
{
|
|
||||||
// Transfer non-duplicate data
|
|
||||||
$this->transferData($toKeep, $toDelete);
|
|
||||||
|
|
||||||
// Update linked entities
|
|
||||||
$this->updateReferences($toKeep, $toDelete);
|
|
||||||
|
|
||||||
$this->em->remove($toDelete);
|
|
||||||
$this->em->flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function transferData(AccompanyingPeriodWork $toKeep, AccompanyingPeriodWork $toDelete): void
|
|
||||||
{
|
{
|
||||||
$conn = $this->em->getConnection();
|
$conn = $this->em->getConnection();
|
||||||
|
|
||||||
$sqlStatements = [
|
|
||||||
$this->generateStartDateSQL(),
|
|
||||||
$this->generateEndDateSQL(),
|
|
||||||
$this->generateCommentSQL(),
|
|
||||||
];
|
|
||||||
|
|
||||||
$conn->beginTransaction();
|
$conn->beginTransaction();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
foreach ($sqlStatements as $sql) {
|
$queries = array_merge(
|
||||||
$conn->executeQuery($sql, ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()]);
|
$this->updateReferences($toKeep, $toDelete),
|
||||||
|
$this->generateStartDateSQL($toDelete, $toKeep),
|
||||||
|
$this->generateEndDateSQL($toDelete, $toKeep),
|
||||||
|
$this->removeAccompanyingPeriodWork($toDelete)
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($queries as $query) {
|
||||||
|
$conn->executeStatement($query['sql'], $query['params']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$conn->commit();
|
$conn->commit();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
dump($e->getMessage());
|
||||||
$conn->rollBack();
|
$conn->rollBack();
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generateStartDateSQL(): string
|
private function generateStartDateSQL(AccompanyingPeriodWork $toDelete, AccompanyingPeriodWork $toKeep): array
|
||||||
{
|
{
|
||||||
return '
|
$queries = [];
|
||||||
UPDATE chill_person_accompanying_period_work
|
$queries[] = [
|
||||||
|
'sql' => 'UPDATE chill_person_accompanying_period_work
|
||||||
SET startdate = LEAST(
|
SET startdate = LEAST(
|
||||||
COALESCE((SELECT startdate FROM chill_person_accompanying_period_work WHERE id = :toDelete), startdate),
|
COALESCE((SELECT startdate FROM chill_person_accompanying_period_work WHERE id = :toDelete), startdate),
|
||||||
startdate
|
startdate
|
||||||
)
|
)
|
||||||
WHERE id = :toKeep';
|
WHERE id = :toKeep',
|
||||||
|
'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()],
|
||||||
|
];
|
||||||
|
|
||||||
|
return $queries;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generateEndDateSQL(): string
|
private function generateEndDateSQL(AccompanyingPeriodWork $toDelete, AccompanyingPeriodWork $toKeep): array
|
||||||
{
|
{
|
||||||
return '
|
$queries = [];
|
||||||
|
$queries[] = [
|
||||||
|
'sql' => '
|
||||||
UPDATE chill_person_accompanying_period_work
|
UPDATE chill_person_accompanying_period_work
|
||||||
SET enddate =
|
SET enddate =
|
||||||
CASE
|
CASE
|
||||||
@ -86,89 +81,71 @@ class AccompanyingPeriodWorkMergeService
|
|||||||
enddate
|
enddate
|
||||||
)
|
)
|
||||||
END
|
END
|
||||||
WHERE id = :toKeep';
|
WHERE id = :toKeep',
|
||||||
|
'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()],
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
return $queries;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generateCommentSQL(): string
|
private function updateReferences(AccompanyingPeriodWork $toKeep, AccompanyingPeriodWork $toDelete): array
|
||||||
{
|
|
||||||
return "
|
|
||||||
UPDATE chill_person_accompanying_period_work
|
|
||||||
SET note = CONCAT_WS(
|
|
||||||
'\n',
|
|
||||||
NULLIF(TRIM(note), ''),
|
|
||||||
NULLIF(TRIM((SELECT note FROM chill_person_accompanying_period_work WHERE id = :toDelete)), '')
|
|
||||||
)
|
|
||||||
WHERE id = :toKeep";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function updateReferences(AccompanyingPeriodWork $toKeep, AccompanyingPeriodWork $toDelete): void
|
|
||||||
{
|
{
|
||||||
|
$queries = [];
|
||||||
$allMeta = $this->em->getMetadataFactory()->getAllMetadata();
|
$allMeta = $this->em->getMetadataFactory()->getAllMetadata();
|
||||||
$conn = $this->em->getConnection();
|
|
||||||
$sqlStatements = [];
|
|
||||||
|
|
||||||
foreach ($allMeta as $meta) {
|
foreach ($allMeta as $meta) {
|
||||||
if ($meta->isMappedSuperclass) {
|
if ($meta->isMappedSuperclass) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($meta->getAssociationMappings() as $assoc) {
|
|
||||||
if (AccompanyingPeriodWork::class !== $assoc['targetEntity']) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($assoc['type'] & ClassMetadata::TO_ONE) {
|
|
||||||
if ('handlingThirdparty' === $assoc['fieldName']) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$sqlStatements[] = $this->generateToOneUpdateQuery($meta, $assoc);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($assoc['type'] & ClassMetadata::TO_MANY) {
|
|
||||||
if (!isset($assoc['joinTable'])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$sqlStatements = array_merge($sqlStatements, $this->generateToManyUpdateQueries($assoc));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$conn->beginTransaction();
|
|
||||||
try {
|
|
||||||
foreach ($sqlStatements as $sql) {
|
|
||||||
$conn->executeStatement($sql, ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()]);
|
|
||||||
}
|
|
||||||
$conn->commit();
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$conn->rollBack();
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function generateToOneUpdateQuery(ClassMetadata $meta, array $assoc): string
|
|
||||||
{
|
|
||||||
$tableName = $meta->getTableName();
|
$tableName = $meta->getTableName();
|
||||||
|
foreach ($meta->getAssociationMappings() as $assoc) {
|
||||||
|
|
||||||
|
if (AccompanyingPeriodWork::class !== $assoc['targetEntity'] && AccompanyingPeriodWork::class !== $assoc['sourceEntity']) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) {
|
||||||
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
|
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
|
||||||
|
|
||||||
return "UPDATE {$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete";
|
if (AccompanyingPeriodWorkReferrerHistory::class === $assoc['sourceEntity']) {
|
||||||
|
$queries[] = [
|
||||||
|
'sql' => "DELETE FROM {$tableName} WHERE {$joinColumn} = :toDelete",
|
||||||
|
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generateToManyUpdateQueries(array $assoc): array
|
$queries[] = [
|
||||||
{
|
'sql' => "UPDATE {$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
|
||||||
$sqls = [];
|
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||||
|
];
|
||||||
|
} elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) {
|
||||||
|
if ($assoc['isOwningSide']) {
|
||||||
$joinTable = $assoc['joinTable']['name'];
|
$joinTable = $assoc['joinTable']['name'];
|
||||||
$owningColumn = $assoc['joinTable']['joinColumns'][0]['name'];
|
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
|
||||||
$inverseColumn = $assoc['joinTable']['inverseJoinColumns'][0]['name'];
|
$queries[] = [
|
||||||
|
'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete AND NOT EXISTS (SELECT 1 FROM {$joinTable} WHERE {$joinColumn} = :toKeep)",
|
||||||
|
'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()],
|
||||||
|
];
|
||||||
|
|
||||||
// Insert relations, skip already existing ones
|
$queries[] = [
|
||||||
$sqls[] = "INSERT IGNORE INTO {$joinTable} ({$owningColumn}, {$inverseColumn})
|
'sql' => "DELETE FROM {$joinTable} WHERE {$joinColumn} = :toDelete",
|
||||||
SELECT :toKeep, {$inverseColumn} FROM {$joinTable} WHERE {$owningColumn} = :toDelete";
|
'params' => ['toDelete' => $toDelete->getId()],
|
||||||
// Delete old references
|
];
|
||||||
$sqls[] = "DELETE FROM {$joinTable} WHERE {$owningColumn} = :toDelete";
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $sqls;
|
return $queries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeAccompanyingPeriodWork(AccompanyingPeriodWork $toDelete): array
|
||||||
|
{
|
||||||
|
return [[
|
||||||
|
'sql' => 'DELETE FROM chill_person_accompanying_period_work WHERE id = :toDelete',
|
||||||
|
'params' => ['toDelete' => $toDelete->getId()],
|
||||||
|
]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user