feat: add UserMovie join entity

This commit is contained in:
thibaud-leclere
2026-03-29 10:10:27 +02:00
parent 5d16d28c59
commit 5f7ddcd3cc
3 changed files with 113 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
<?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 Version20260329000001 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 user_movie (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, user_id INT NOT NULL, movie_id INT NOT NULL, PRIMARY KEY (id))');
$this->addSql('CREATE INDEX IDX_A6B68B33A76ED395 ON user_movie (user_id)');
$this->addSql('CREATE INDEX IDX_A6B68B338F93B6FC ON user_movie (movie_id)');
$this->addSql('CREATE UNIQUE INDEX user_movie_unique ON user_movie (user_id, movie_id)');
$this->addSql('ALTER TABLE user_movie ADD CONSTRAINT FK_A6B68B33A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_movie ADD CONSTRAINT FK_A6B68B338F93B6FC FOREIGN KEY (movie_id) REFERENCES movie (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user_movie DROP CONSTRAINT FK_A6B68B33A76ED395');
$this->addSql('ALTER TABLE user_movie DROP CONSTRAINT FK_A6B68B338F93B6FC');
$this->addSql('DROP TABLE user_movie');
}
}

55
src/Entity/UserMovie.php Normal file
View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\UserMovieRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: UserMovieRepository::class)]
#[ORM\UniqueConstraint(name: 'user_movie_unique', columns: ['user_id', 'movie_id'])]
class UserMovie
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
#[ORM\ManyToOne(targetEntity: Movie::class)]
#[ORM\JoinColumn(nullable: false)]
private ?Movie $movie = null;
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
public function getMovie(): ?Movie
{
return $this->movie;
}
public function setMovie(?Movie $movie): static
{
$this->movie = $movie;
return $this;
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Entity\UserMovie;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<UserMovie>
*/
class UserMovieRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, UserMovie::class);
}
}