mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
FEATURE [relationships] Add personMoveRelationHandler and test for this
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Actions\Remove\Handler;
|
||||
|
||||
use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relationship;
|
||||
|
||||
class PersonMoveRelationHandler implements PersonMoveSqlHandlerInterface
|
||||
{
|
||||
public function supports(string $className, string $field): bool
|
||||
{
|
||||
return $className === Relationship::class;
|
||||
}
|
||||
|
||||
public function getSqls(string $className, string $field, Person $from, Person $to): array
|
||||
{
|
||||
$insertSql = sprintf(<<<SQL
|
||||
INSERT INTO chill_person_relationships (id, relation_id, reverse, fromperson_id, toperson_id)
|
||||
SELECT nextval('chill_person_relationships_id_seq'), relation_id, reverse, fromperson_id, toperson_id
|
||||
FROM chill_person_relationships cpr
|
||||
WHERE fromperson_id = %d OR toperson_id = %d
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM chill_person_relationships cpr2
|
||||
WHERE
|
||||
cpr2.fromperson_id = %d AND cpr2.toperson_id = %d
|
||||
OR cpr2.fromperson_id = %d AND cpr2.toperson_id = %d
|
||||
)
|
||||
SQL, $from->getId(), $from->getId(), $to->getId(), $from->getId(), $from->getId(), $to->getId());
|
||||
|
||||
$deleteSql = sprintf(<<<SQL
|
||||
DELETE FROM chill_person_relationships WHERE fromperson_id = %d;
|
||||
DELETE FROM chill_person_relationships WHERE toperson_id = %d;
|
||||
SQL, $from->getId(), $from->getId());
|
||||
|
||||
return [$insertSql, $deleteSql];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user