diff --git a/src/Bundle/ChillTicketBundle/src/Entity/Motive.php b/src/Bundle/ChillTicketBundle/src/Entity/Motive.php index 789d6b562..2852bdc22 100644 --- a/src/Bundle/ChillTicketBundle/src/Entity/Motive.php +++ b/src/Bundle/ChillTicketBundle/src/Entity/Motive.php @@ -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); + } } diff --git a/src/Bundle/ChillTicketBundle/src/migrations/Version20250924124214.php b/src/Bundle/ChillTicketBundle/src/migrations/Version20250924124214.php new file mode 100644 index 000000000..0af41c592 --- /dev/null +++ b/src/Bundle/ChillTicketBundle/src/migrations/Version20250924124214.php @@ -0,0 +1,37 @@ +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'); + } +}