Add parent / child relationship top motives

This commit is contained in:
2025-09-24 14:45:13 +02:00
parent 38935edb93
commit 0e1ec389a5
2 changed files with 84 additions and 0 deletions

View File

@@ -52,9 +52,17 @@ class Motive
#[Serializer\Groups(['read'])]
private array $supplementaryComments = [];
#[ORM\ManyToOne(targetEntity: Motive::class, inversedBy: 'children')]
private ?Motive $parent = null;
#[ORM\OneToMany(targetEntity: Motive::class, mappedBy: 'motive')]
private Collection $children;
public function __construct()
{
$this->storedObjects = new ArrayCollection();
$this->children = new ArrayCollection();
}
public function addStoredObject(StoredObject $storedObject): void
@@ -142,4 +150,43 @@ class Motive
$this->supplementaryComments[$key] = $supplementaryComment;
}
}
public function isParent(): bool
{
return $this->children->count() > 0;
}
public function isChild(): bool
{
return null !== $this->parent;
}
public function setParent(?Motive $parent): void
{
if (null !== $parent) {
$parent->addChild($this);
} else {
$this->parent->removeChild($this);
}
$this->parent = $parent;
}
/**
* @internal use @see{setParent} instead
*/
public function addChild(Motive $child): void
{
if (!$this->children->contains($child)) {
$this->children->add($child);
}
}
/**
* @internal use @see{setParent} with null as argument instead
*/
public function removeChild(Motive $child): void
{
$this->children->removeElement($child);
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\Ticket;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20250924124214 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add parent to motive';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_ticket.motive ADD parent_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE chill_ticket.motive ADD CONSTRAINT FK_DE298BF8727ACA70 FOREIGN KEY (parent_id) REFERENCES chill_ticket.motive (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_DE298BF8727ACA70 ON chill_ticket.motive (parent_id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_ticket.motive DROP CONSTRAINT FK_DE298BF8727ACA70');
$this->addSql('DROP INDEX chill_ticket.IDX_DE298BF8727ACA70');
$this->addSql('ALTER TABLE chill_ticket.motive DROP parent_id');
}
}