From bdd2ef127201709ea121b73d7ac3296451f22467 Mon Sep 17 00:00:00 2001
From: Eric WAGNER <eric.wagner@etu.unistra.fr>
Date: Wed, 29 Mar 2023 22:03:35 +0200
Subject: [PATCH] Ajout des pilotes et Ecurie

---
 migrations/Version20230329200327.php | 35 +++++++++++
 src/Entity/Ecurie.php                | 90 ++++++++++++++++++++++++++++
 src/Entity/Pilotes.php               | 66 ++++++++++++++++++++
 src/Repository/EcurieRepository.php  | 66 ++++++++++++++++++++
 src/Repository/PilotesRepository.php | 66 ++++++++++++++++++++
 5 files changed, 323 insertions(+)
 create mode 100644 migrations/Version20230329200327.php
 create mode 100644 src/Entity/Ecurie.php
 create mode 100644 src/Entity/Pilotes.php
 create mode 100644 src/Repository/EcurieRepository.php
 create mode 100644 src/Repository/PilotesRepository.php

diff --git a/migrations/Version20230329200327.php b/migrations/Version20230329200327.php
new file mode 100644
index 0000000..897b3ae
--- /dev/null
+++ b/migrations/Version20230329200327.php
@@ -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 Version20230329200327 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 ecurie (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, picture VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
+        $this->addSql('CREATE TABLE pilotes (id INT AUTO_INCREMENT NOT NULL, ecurie_id INT NOT NULL, name VARCHAR(255) NOT NULL, picture VARCHAR(255) NOT NULL, INDEX IDX_E306533657F92A74 (ecurie_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
+        $this->addSql('ALTER TABLE pilotes ADD CONSTRAINT FK_E306533657F92A74 FOREIGN KEY (ecurie_id) REFERENCES ecurie (id)');
+    }
+
+    public function down(Schema $schema): void
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->addSql('ALTER TABLE pilotes DROP FOREIGN KEY FK_E306533657F92A74');
+        $this->addSql('DROP TABLE ecurie');
+        $this->addSql('DROP TABLE pilotes');
+    }
+}
diff --git a/src/Entity/Ecurie.php b/src/Entity/Ecurie.php
new file mode 100644
index 0000000..672b511
--- /dev/null
+++ b/src/Entity/Ecurie.php
@@ -0,0 +1,90 @@
+<?php
+
+namespace App\Entity;
+
+use App\Repository\EcurieRepository;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
+use Doctrine\ORM\Mapping as ORM;
+
+#[ORM\Entity(repositoryClass: EcurieRepository::class)]
+class Ecurie
+{
+    #[ORM\Id]
+    #[ORM\GeneratedValue]
+    #[ORM\Column]
+    private ?int $id = null;
+
+    #[ORM\Column(length: 255)]
+    private ?string $name = null;
+
+    #[ORM\Column(length: 255)]
+    private ?string $picture = null;
+
+    #[ORM\OneToMany(mappedBy: 'ecurie', targetEntity: Pilotes::class, orphanRemoval: true)]
+    private Collection $pilotes;
+
+    public function __construct()
+    {
+        $this->pilotes = new ArrayCollection();
+    }
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+    public function getName(): ?string
+    {
+        return $this->name;
+    }
+
+    public function setName(string $name): self
+    {
+        $this->name = $name;
+
+        return $this;
+    }
+
+    public function getPicture(): ?string
+    {
+        return $this->picture;
+    }
+
+    public function setPicture(string $picture): self
+    {
+        $this->picture = $picture;
+
+        return $this;
+    }
+
+    /**
+     * @return Collection<int, Pilotes>
+     */
+    public function getPilotes(): Collection
+    {
+        return $this->pilotes;
+    }
+
+    public function addPilote(Pilotes $pilote): self
+    {
+        if (!$this->pilotes->contains($pilote)) {
+            $this->pilotes->add($pilote);
+            $pilote->setEcurie($this);
+        }
+
+        return $this;
+    }
+
+    public function removePilote(Pilotes $pilote): self
+    {
+        if ($this->pilotes->removeElement($pilote)) {
+            // set the owning side to null (unless already changed)
+            if ($pilote->getEcurie() === $this) {
+                $pilote->setEcurie(null);
+            }
+        }
+
+        return $this;
+    }
+}
diff --git a/src/Entity/Pilotes.php b/src/Entity/Pilotes.php
new file mode 100644
index 0000000..56c2c51
--- /dev/null
+++ b/src/Entity/Pilotes.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Entity;
+
+use App\Repository\PilotesRepository;
+use Doctrine\ORM\Mapping as ORM;
+
+#[ORM\Entity(repositoryClass: PilotesRepository::class)]
+class Pilotes
+{
+    #[ORM\Id]
+    #[ORM\GeneratedValue]
+    #[ORM\Column]
+    private ?int $id = null;
+
+    #[ORM\Column(length: 255)]
+    private ?string $name = null;
+
+    #[ORM\Column(length: 255)]
+    private ?string $picture = null;
+
+    #[ORM\ManyToOne(inversedBy: 'pilotes')]
+    #[ORM\JoinColumn(nullable: false)]
+    private ?Ecurie $ecurie = null;
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+    public function getName(): ?string
+    {
+        return $this->name;
+    }
+
+    public function setName(string $name): self
+    {
+        $this->name = $name;
+
+        return $this;
+    }
+
+    public function getPicture(): ?string
+    {
+        return $this->picture;
+    }
+
+    public function setPicture(string $picture): self
+    {
+        $this->picture = $picture;
+
+        return $this;
+    }
+
+    public function getEcurie(): ?Ecurie
+    {
+        return $this->ecurie;
+    }
+
+    public function setEcurie(?Ecurie $ecurie): self
+    {
+        $this->ecurie = $ecurie;
+
+        return $this;
+    }
+}
diff --git a/src/Repository/EcurieRepository.php b/src/Repository/EcurieRepository.php
new file mode 100644
index 0000000..cd9e80e
--- /dev/null
+++ b/src/Repository/EcurieRepository.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Repository;
+
+use App\Entity\Ecurie;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Persistence\ManagerRegistry;
+
+/**
+ * @extends ServiceEntityRepository<Ecurie>
+ *
+ * @method Ecurie|null find($id, $lockMode = null, $lockVersion = null)
+ * @method Ecurie|null findOneBy(array $criteria, array $orderBy = null)
+ * @method Ecurie[]    findAll()
+ * @method Ecurie[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
+ */
+class EcurieRepository extends ServiceEntityRepository
+{
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, Ecurie::class);
+    }
+
+    public function save(Ecurie $entity, bool $flush = false): void
+    {
+        $this->getEntityManager()->persist($entity);
+
+        if ($flush) {
+            $this->getEntityManager()->flush();
+        }
+    }
+
+    public function remove(Ecurie $entity, bool $flush = false): void
+    {
+        $this->getEntityManager()->remove($entity);
+
+        if ($flush) {
+            $this->getEntityManager()->flush();
+        }
+    }
+
+//    /**
+//     * @return Ecurie[] Returns an array of Ecurie objects
+//     */
+//    public function findByExampleField($value): array
+//    {
+//        return $this->createQueryBuilder('e')
+//            ->andWhere('e.exampleField = :val')
+//            ->setParameter('val', $value)
+//            ->orderBy('e.id', 'ASC')
+//            ->setMaxResults(10)
+//            ->getQuery()
+//            ->getResult()
+//        ;
+//    }
+
+//    public function findOneBySomeField($value): ?Ecurie
+//    {
+//        return $this->createQueryBuilder('e')
+//            ->andWhere('e.exampleField = :val')
+//            ->setParameter('val', $value)
+//            ->getQuery()
+//            ->getOneOrNullResult()
+//        ;
+//    }
+}
diff --git a/src/Repository/PilotesRepository.php b/src/Repository/PilotesRepository.php
new file mode 100644
index 0000000..205012d
--- /dev/null
+++ b/src/Repository/PilotesRepository.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Repository;
+
+use App\Entity\Pilotes;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Persistence\ManagerRegistry;
+
+/**
+ * @extends ServiceEntityRepository<Pilotes>
+ *
+ * @method Pilotes|null find($id, $lockMode = null, $lockVersion = null)
+ * @method Pilotes|null findOneBy(array $criteria, array $orderBy = null)
+ * @method Pilotes[]    findAll()
+ * @method Pilotes[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
+ */
+class PilotesRepository extends ServiceEntityRepository
+{
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, Pilotes::class);
+    }
+
+    public function save(Pilotes $entity, bool $flush = false): void
+    {
+        $this->getEntityManager()->persist($entity);
+
+        if ($flush) {
+            $this->getEntityManager()->flush();
+        }
+    }
+
+    public function remove(Pilotes $entity, bool $flush = false): void
+    {
+        $this->getEntityManager()->remove($entity);
+
+        if ($flush) {
+            $this->getEntityManager()->flush();
+        }
+    }
+
+//    /**
+//     * @return Pilotes[] Returns an array of Pilotes objects
+//     */
+//    public function findByExampleField($value): array
+//    {
+//        return $this->createQueryBuilder('p')
+//            ->andWhere('p.exampleField = :val')
+//            ->setParameter('val', $value)
+//            ->orderBy('p.id', 'ASC')
+//            ->setMaxResults(10)
+//            ->getQuery()
+//            ->getResult()
+//        ;
+//    }
+
+//    public function findOneBySomeField($value): ?Pilotes
+//    {
+//        return $this->createQueryBuilder('p')
+//            ->andWhere('p.exampleField = :val')
+//            ->setParameter('val', $value)
+//            ->getQuery()
+//            ->getOneOrNullResult()
+//        ;
+//    }
+}
-- 
GitLab