From b16ff9743a046389e273a9ae5d30d1eea9331773 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 16 Oct 2025 14:47:05 +0200 Subject: [PATCH 1/2] add link attribute to Chapter --- migrations/Version20251016124505.php | 35 +++++++++++++++++ src/Entity/Chapter.php | 58 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 migrations/Version20251016124505.php diff --git a/migrations/Version20251016124505.php b/migrations/Version20251016124505.php new file mode 100644 index 0000000..5a7d9b5 --- /dev/null +++ b/migrations/Version20251016124505.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE chapter ADD linked_by_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE chapter ADD CONSTRAINT FK_F981B52E1AE3CFF3 FOREIGN KEY (linked_by_id) REFERENCES chapter (id)'); + $this->addSql('CREATE INDEX IDX_F981B52E1AE3CFF3 ON chapter (linked_by_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE chapter DROP FOREIGN KEY FK_F981B52E1AE3CFF3'); + $this->addSql('DROP INDEX IDX_F981B52E1AE3CFF3 ON chapter'); + $this->addSql('ALTER TABLE chapter DROP linked_by_id'); + } +} diff --git a/src/Entity/Chapter.php b/src/Entity/Chapter.php index 4ece5d3..a3f2824 100644 --- a/src/Entity/Chapter.php +++ b/src/Entity/Chapter.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\ChapterRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: ChapterRepository::class)] @@ -21,6 +23,20 @@ class Chapter #[ORM\JoinColumn(nullable: false)] private ?Section $section = null; + #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'link')] + private ?self $linkedBy = null; + + /** + * @var Collection + */ + #[ORM\OneToMany(targetEntity: self::class, mappedBy: 'chapter')] + private Collection $link; + + public function __construct() + { + $this->link = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -49,4 +65,46 @@ public function setSection(?Section $section): static return $this; } + + public function getLinkedBy(): ?self + { + return $this->linkedBy; + } + + public function setLinkedBy(?self $linkedBy): static + { + $this->linkedBy = $linkedBy; + + return $this; + } + + /** + * @return Collection + */ + public function getLink(): Collection + { + return $this->link; + } + + public function addLink(self $link): static + { + if (!$this->link->contains($link)) { + $this->link->add($link); + $link->setLinkedBy($this); + } + + return $this; + } + + public function removeLink(self $link): static + { + if ($this->link->removeElement($link)) { + // set the owning side to null (unless already changed) + if ($link->getLinkedBy() === $this) { + $link->setLinkedBy(null); + } + } + + return $this; + } } From 661dd616e7bbc74387bafcc808101833d00201bf Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 16 Oct 2025 14:48:35 +0200 Subject: [PATCH 2/2] update Fixtures --- src/DataFixtures/ChapterFixtures.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DataFixtures/ChapterFixtures.php b/src/DataFixtures/ChapterFixtures.php index fd07a77..b0c136c 100644 --- a/src/DataFixtures/ChapterFixtures.php +++ b/src/DataFixtures/ChapterFixtures.php @@ -22,6 +22,7 @@ public function load(ObjectManager $manager): void $chapter2->setSection($this->getReference(SectionFixtures::SEC_TWO_REFERENCE, Section::class)); $chapter2->setCategory($this->getReference(CategoryFixtures::CAT_TWO_REFERENCE, Category::class)); + $chapter2->addLink($chapter1); $manager->persist($chapter1); $manager->persist($chapter2);