Skip to content
Closed
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/Version20251016124505.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 Version20251016124505 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('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)');
Comment on lines +22 to +25

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu dois faire valider tes noms de champs avant de faire une migration : là tu dois supprimer cette migration vu que le nom du champ ne convient pas

}

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');
}
}
1 change: 1 addition & 0 deletions src/DataFixtures/ChapterFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
58 changes: 58 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,20 @@ class Chapter
#[ORM\JoinColumn(nullable: false)]
private ?Section $section = null;

#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'link')]
private ?self $linkedBy = null;

/**
* @var Collection<int, self>
*/
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'chapter')]
private Collection $link;
Comment on lines +32 to +33

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link est un mot singulier dont la sémantique ne représente pas une collection : le nom de variable en convient pas


public function __construct()
{
$this->link = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -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<int, self>
*/
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;
}
}