From 51d69269ca34bb2feb7ea9f13ef50b062ef415b8 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 16 Oct 2025 15:44:18 +0200 Subject: [PATCH 1/2] add links between Chapter as attributes --- migrations/Version20251016134343.php | 35 ++++++++++++++ src/Entity/Chapter.php | 71 ++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 migrations/Version20251016134343.php diff --git a/migrations/Version20251016134343.php b/migrations/Version20251016134343.php new file mode 100644 index 0000000..2563803 --- /dev/null +++ b/migrations/Version20251016134343.php @@ -0,0 +1,35 @@ +addSql('CREATE TABLE chapter_chapter (chapter_source INT NOT NULL, chapter_target INT NOT NULL, INDEX IDX_83B0B1614D498F8 (chapter_source), INDEX IDX_83B0B1611D31C877 (chapter_target), PRIMARY KEY(chapter_source, chapter_target)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE chapter_chapter ADD CONSTRAINT FK_83B0B1614D498F8 FOREIGN KEY (chapter_source) REFERENCES chapter (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE chapter_chapter ADD CONSTRAINT FK_83B0B1611D31C877 FOREIGN KEY (chapter_target) REFERENCES chapter (id) ON DELETE CASCADE'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE chapter_chapter DROP FOREIGN KEY FK_83B0B1614D498F8'); + $this->addSql('ALTER TABLE chapter_chapter DROP FOREIGN KEY FK_83B0B1611D31C877'); + $this->addSql('DROP TABLE chapter_chapter'); + } +} diff --git a/src/Entity/Chapter.php b/src/Entity/Chapter.php index 4ece5d3..fd2dfb6 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,24 @@ class Chapter #[ORM\JoinColumn(nullable: false)] private ?Section $section = null; + /** + * @var Collection + */ + #[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'previousChapters')] + private Collection $followingChapters; + + /** + * @var Collection + */ + #[ORM\ManyToMany(targetEntity: self::class, mappedBy: 'followingChapters')] + private Collection $previousChapters; + + public function __construct() + { + $this->followingChapters = new ArrayCollection(); + $this->previousChapters = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -49,4 +69,55 @@ public function setSection(?Section $section): static return $this; } + + /** + * @return Collection + */ + public function getFollowingChapters(): Collection + { + return $this->followingChapters; + } + + public function addFollowingChapter(self $followingChapter): static + { + if (!$this->followingChapters->contains($followingChapter)) { + $this->followingChapters->add($followingChapter); + } + + return $this; + } + + public function removeFollowingChapter(self $followingChapter): static + { + $this->followingChapters->removeElement($followingChapter); + + return $this; + } + + /** + * @return Collection + */ + public function getPreviousChapters(): Collection + { + return $this->previousChapters; + } + + public function addPreviousChapter(self $previousChapter): static + { + if (!$this->previousChapters->contains($previousChapter)) { + $this->previousChapters->add($previousChapter); + $previousChapter->addFollowingChapter($this); + } + + return $this; + } + + public function removePreviousChapter(self $previousChapter): static + { + if ($this->previousChapters->removeElement($previousChapter)) { + $previousChapter->removeFollowingChapter($this); + } + + return $this; + } } From 09c62d21f93c3c20820af76378daa8b4bde70ace Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 16 Oct 2025 15:48:56 +0200 Subject: [PATCH 2/2] update fixtures --- src/DataFixtures/ChapterFixtures.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DataFixtures/ChapterFixtures.php b/src/DataFixtures/ChapterFixtures.php index fd07a77..14377c6 100644 --- a/src/DataFixtures/ChapterFixtures.php +++ b/src/DataFixtures/ChapterFixtures.php @@ -23,6 +23,8 @@ 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)); + $chapter1->addFollowingChapter($chapter2); + $manager->persist($chapter1); $manager->persist($chapter2); $manager->flush();