mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
[centerHistory] add conditions to be sure at least one center history exists per person
This commit is contained in:
parent
f02d97ddb0
commit
2921073da3
@ -20,10 +20,11 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
|
|||||||
public function getSqls(string $className, string $field, Person $from, Person $to): array
|
public function getSqls(string $className, string $field, Person $from, Person $to): array
|
||||||
{
|
{
|
||||||
$sqlStatements = [];
|
$sqlStatements = [];
|
||||||
|
$oldestDateA = null;
|
||||||
|
$oldestDateB = null;
|
||||||
|
|
||||||
$centerHistoriesA = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $from->getId()]);
|
$centerHistoriesA = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $from->getId()]);
|
||||||
// $centerHistoriesA = $from->getCenterHistory()->toArray();
|
// $centerHistoriesA = $from->getCenterHistory()->toArray();
|
||||||
// var_dump($centerHistoriesA);
|
|
||||||
$datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA);
|
$datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA);
|
||||||
if (!count($datesArrayA) === 0) {
|
if (!count($datesArrayA) === 0) {
|
||||||
$oldestDateA = min($datesArrayA);
|
$oldestDateA = min($datesArrayA);
|
||||||
@ -31,7 +32,6 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
|
|||||||
|
|
||||||
$centerHistoriesB = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $to->getId()]);
|
$centerHistoriesB = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $to->getId()]);
|
||||||
// $centerHistoriesB = $to->getCenterHistory()->toArray();
|
// $centerHistoriesB = $to->getCenterHistory()->toArray();
|
||||||
// var_dump($centerHistoriesB);
|
|
||||||
$datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB);
|
$datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB);
|
||||||
|
|
||||||
if (!count($datesArrayB) === 0) {
|
if (!count($datesArrayB) === 0) {
|
||||||
@ -45,12 +45,13 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
|
|||||||
|
|
||||||
$sqlStatements = [$sqlDelete];
|
$sqlStatements = [$sqlDelete];
|
||||||
|
|
||||||
if ($oldestDateA <= $oldestDateB) {
|
if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) {
|
||||||
$sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId());
|
$sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId());
|
||||||
|
|
||||||
$sqlStatements = [$sqlInsert, $sqlDelete];
|
$sqlStatements = [$sqlInsert, $sqlDelete];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var_dump($sqlStatements);
|
||||||
return $sqlStatements;
|
return $sqlStatements;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -61,11 +61,11 @@ class PersonMoveTest extends KernelTestCase
|
|||||||
$move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher);
|
$move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher);
|
||||||
$sqls = $move->getSQL($personA, $personB);
|
$sqls = $move->getSQL($personA, $personB);
|
||||||
$conn = $this->em->getConnection();
|
$conn = $this->em->getConnection();
|
||||||
// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) {
|
$this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) {
|
||||||
foreach ($sqls as $sql) {
|
foreach ($sqls as $sql) {
|
||||||
$conn->executeStatement($sql);
|
$conn->executeStatement($sql);
|
||||||
}
|
}
|
||||||
// });
|
});
|
||||||
|
|
||||||
$personA = $this->em->find(Person::class, $personA->getId());
|
$personA = $this->em->find(Person::class, $personA->getId());
|
||||||
$personB = $this->em->find(Person::class, $personB->getId());
|
$personB = $this->em->find(Person::class, $personB->getId());
|
||||||
@ -100,11 +100,11 @@ class PersonMoveTest extends KernelTestCase
|
|||||||
$move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher);
|
$move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher);
|
||||||
$sqls = $move->getSQL($personA, $personB);
|
$sqls = $move->getSQL($personA, $personB);
|
||||||
$conn = $this->em->getConnection();
|
$conn = $this->em->getConnection();
|
||||||
// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) {
|
$this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) {
|
||||||
foreach ($sqls as $sql) {
|
foreach ($sqls as $sql) {
|
||||||
$conn->executeStatement($sql);
|
$conn->executeStatement($sql);
|
||||||
}
|
}
|
||||||
// });
|
});
|
||||||
|
|
||||||
$personA = $this->em->find(Person::class, $personA->getId());
|
$personA = $this->em->find(Person::class, $personA->getId());
|
||||||
$personB = $this->em->find(Person::class, $personB->getId());
|
$personB = $this->em->find(Person::class, $personB->getId());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user