Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions migrations/Version20251016134343.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20251016134343 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
2 changes: 2 additions & 0 deletions src/DataFixtures/ChapterFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
71 changes: 71 additions & 0 deletions src/Entity/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -21,6 +23,24 @@ class Chapter
#[ORM\JoinColumn(nullable: false)]
private ?Section $section = null;

/**
* @var Collection<int, self>
*/
#[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'previousChapters')]
private Collection $followingChapters;

/**
* @var Collection<int, self>
*/
#[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;
Expand Down Expand Up @@ -49,4 +69,55 @@ public function setSection(?Section $section): static

return $this;
}

/**
* @return Collection<int, self>
*/
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<int, self>
*/
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;
}
}