diff --git a/README.md b/README.md index d807762..8d6ae50 100644 --- a/README.md +++ b/README.md @@ -78,4 +78,11 @@ symfony console d:f:l Pas de commandes, juste du code dans AtelierController ### Question 10 et 11 -Pas de commandes. \ No newline at end of file +Pas de commandes. + +### Question 12 +```bash +symfony console make:entity +symfony console make:migration +symfony console d:m:m +``` \ No newline at end of file diff --git a/migrations/Version20230209092109.php b/migrations/Version20230209092109.php new file mode 100644 index 0000000..fc17dda --- /dev/null +++ b/migrations/Version20230209092109.php @@ -0,0 +1,33 @@ +addSql('CREATE TABLE user_atelier (user_id INTEGER NOT NULL, atelier_id INTEGER NOT NULL, PRIMARY KEY(user_id, atelier_id), CONSTRAINT FK_B9B60629A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_B9B6062982E2CF35 FOREIGN KEY (atelier_id) REFERENCES atelier (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_B9B60629A76ED395 ON user_atelier (user_id)'); + $this->addSql('CREATE INDEX IDX_B9B6062982E2CF35 ON user_atelier (atelier_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE user_atelier'); + } +} diff --git a/src/Entity/Atelier.php b/src/Entity/Atelier.php index 1bbe61e..2035da6 100644 --- a/src/Entity/Atelier.php +++ b/src/Entity/Atelier.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\AtelierRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: AtelierRepository::class)] @@ -19,10 +21,18 @@ class Atelier #[ORM\Column(length: 1024, nullable: true)] private ?string $description = null; - #[ORM\ManyToOne(inversedBy: 'ateliersFormÃÃÃes')] + #[ORM\ManyToOne(inversedBy: 'ateliersForm���es')] #[ORM\JoinColumn(nullable: false)] private ?User $instructeur = null; + #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'formationsSuivies')] + private Collection $eleves; + + public function __construct() + { + $this->eleves = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -63,4 +73,31 @@ class Atelier return $this; } + + /** + * @return Collection + */ + public function getEleves(): Collection + { + return $this->eleves; + } + + public function addEleve(User $eleve): self + { + if (!$this->eleves->contains($eleve)) { + $this->eleves->add($eleve); + $eleve->addFormationsSuivie($this); + } + + return $this; + } + + public function removeEleve(User $eleve): self + { + if ($this->eleves->removeElement($eleve)) { + $eleve->removeFormationsSuivie($this); + } + + return $this; + } } diff --git a/src/Entity/User.php b/src/Entity/User.php index 507d85b..98739b6 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -38,11 +38,15 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface private ?string $prenom = null; #[ORM\OneToMany(mappedBy: 'instructeur', targetEntity: Atelier::class, orphanRemoval: true)] - private Collection $ateliersFormÃÃÃes; + private Collection $ateliersForm���es; + + #[ORM\ManyToMany(targetEntity: atelier::class, inversedBy: 'eleves')] + private Collection $formationsSuivies; public function __construct() { - $this->ateliersFormÃÃÃes = new ArrayCollection(); + $this->ateliersForm���es = new ArrayCollection(); + $this->formationsSuivies = new ArrayCollection(); } public function getId(): ?int @@ -143,15 +147,15 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface /** * @return Collection */ - public function getAteliersFormÃÃÃes(): Collection + public function getAteliersForm���es(): Collection { - return $this->ateliersFormÃÃÃes; + return $this->ateliersForm���es; } public function addAteliersFormE(Atelier $ateliersFormE): self { - if (!$this->ateliersFormÃÃÃes->contains($ateliersFormE)) { - $this->ateliersFormÃÃÃes->add($ateliersFormE); + if (!$this->ateliersForm���es->contains($ateliersFormE)) { + $this->ateliersForm���es->add($ateliersFormE); $ateliersFormE->setInstructeur($this); } @@ -160,7 +164,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface public function removeAteliersFormE(Atelier $ateliersFormE): self { - if ($this->ateliersFormÃÃÃes->removeElement($ateliersFormE)) { + if ($this->ateliersForm���es->removeElement($ateliersFormE)) { // set the owning side to null (unless already changed) if ($ateliersFormE->getInstructeur() === $this) { $ateliersFormE->setInstructeur(null); @@ -169,4 +173,28 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } + + /** + * @return Collection + */ + public function getFormationsSuivies(): Collection + { + return $this->formationsSuivies; + } + + public function addFormationsSuivie(atelier $formationsSuivie): self + { + if (!$this->formationsSuivies->contains($formationsSuivie)) { + $this->formationsSuivies->add($formationsSuivie); + } + + return $this; + } + + public function removeFormationsSuivie(atelier $formationsSuivie): self + { + $this->formationsSuivies->removeElement($formationsSuivie); + + return $this; + } }