Эх сурвалжийг харах

Passage de OneToMany pour les mediaVideos

François Drouhard 1 жил өмнө
parent
commit
1b305f5740

+ 43 - 0
migrations/Version20230419104506.php

@@ -0,0 +1,43 @@
+<?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 Version20230419104506 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 media_video ADD film_id INT NOT NULL');
+
+        foreach($this->connection->fetchAllAssociative('SELECT id, media_video_id FROM film WHERE media_video_id IS NOT NULL') as $film) {
+            $this->addSql('UPDATE media_video SET film_id = :idFilm WHERE id = :mediaVideoIdFilm', [
+                'idFilm' => $film['id'],
+                'mediaVideoIdFilm' => $film['media_video_id']
+            ]);
+        }
+
+        $this->addSql('ALTER TABLE media_video ADD CONSTRAINT FK_63DE1E9D567F5183 FOREIGN KEY (film_id) REFERENCES film (id)');
+        $this->addSql('CREATE INDEX IDX_63DE1E9D567F5183 ON media_video (film_id)');
+    }
+
+    public function down(Schema $schema): void
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->addSql('ALTER TABLE media_video DROP FOREIGN KEY FK_63DE1E9D567F5183');
+        $this->addSql('DROP INDEX IDX_63DE1E9D567F5183 ON media_video');
+        $this->addSql('ALTER TABLE media_video DROP film_id');
+    }
+}

+ 35 - 0
src/Entity/Film.php

@@ -3,6 +3,7 @@
 namespace App\Entity;
 
 use App\Repository\FilmRepository;
+use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\Collection;
 use Doctrine\DBAL\Types\DateImmutableType;
 use Doctrine\DBAL\Types\Types;
@@ -84,6 +85,9 @@ class Film
     #[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)]
     private ?\DateTimeImmutable $dateSortie = null;
 
+    #[ORM\OneToMany(mappedBy: 'film', targetEntity: MediaVideo::class, orphanRemoval: true)]
+    private Collection $mediaVideos;
+
     public function __construct()
     {
         $this->realisateurs = new \Doctrine\Common\Collections\ArrayCollection();
@@ -92,6 +96,7 @@ class Film
         $this->genres = new \Doctrine\Common\Collections\ArrayCollection();
         $this->commentaires = new \Doctrine\Common\Collections\ArrayCollection();
         $this->setDateSubmited(new \DateTime());
+        $this->mediaVideos = new ArrayCollection();
     }
 
     public function getAuthered(): ?User
@@ -322,5 +327,35 @@ class Film
         return $this;
     }
 
+    /**
+     * @return Collection<int, MediaVideo>
+     */
+    public function getMediaVideos(): Collection
+    {
+        return $this->mediaVideos;
+    }
+
+    public function addMediaVideo(MediaVideo $mediaVideo): self
+    {
+        if (!$this->mediaVideos->contains($mediaVideo)) {
+            $this->mediaVideos->add($mediaVideo);
+            $mediaVideo->setFilm($this);
+        }
+
+        return $this;
+    }
+
+    public function removeMediaVideo(MediaVideo $mediaVideo): self
+    {
+        if ($this->mediaVideos->removeElement($mediaVideo)) {
+            // set the owning side to null (unless already changed)
+            if ($mediaVideo->getFilm() === $this) {
+                $mediaVideo->setFilm(null);
+            }
+        }
+
+        return $this;
+    }
+
 
 }

+ 16 - 0
src/Entity/MediaVideo.php

@@ -34,6 +34,10 @@ class MediaVideo
     
     private ?string $url = null;
 
+    #[ORM\ManyToOne(inversedBy: 'mediaVideos')]
+    #[ORM\JoinColumn(nullable: false)]
+    private ?Film $film = null;
+
     public function setFromTmdb(string $type, string $identif): void
     {
         $this->setType($type);
@@ -230,4 +234,16 @@ class MediaVideo
         $video = "<iframe src='".$this->embedUrl()."' allowfullscreen></iframe>";
         return $video;
     }
+
+    public function getFilm(): ?Film
+    {
+        return $this->film;
+    }
+
+    public function setFilm(?Film $film): self
+    {
+        $this->film = $film;
+
+        return $this;
+    }
 }