diff --git a/migrations/Version20211215132322.php b/migrations/Version20211215132322.php new file mode 100644 index 0000000000000000000000000000000000000000..b957dafcde370943e9b7d0477b50ef206ec0db6a --- /dev/null +++ b/migrations/Version20211215132322.php @@ -0,0 +1,36 @@ +<?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 Version20211215132322 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 capsule (id INT AUTO_INCREMENT NOT NULL, nom VARCHAR(255) NOT NULL, aut_crea VARCHAR(255) NOT NULL, dt_crea DATETIME NOT NULL, aut_maj VARCHAR(255) NOT NULL, dt_maj DATETIME NOT NULL, link VARCHAR(255) NOT NULL, edition_link VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE editeur_capsule (capsule_id INT NOT NULL, user_id INT NOT NULL, INDEX IDX_A18592E2714704E9 (capsule_id), INDEX IDX_A18592E2A76ED395 (user_id), PRIMARY KEY(capsule_id, user_id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE editeur_capsule ADD CONSTRAINT FK_A18592E2714704E9 FOREIGN KEY (capsule_id) REFERENCES capsule (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE editeur_capsule ADD CONSTRAINT FK_A18592E2A76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id) ON DELETE CASCADE'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE editeur_capsule DROP FOREIGN KEY FK_A18592E2714704E9'); + $this->addSql('DROP TABLE capsule'); + $this->addSql('DROP TABLE editeur_capsule'); + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index d95519f0a238dd12b15888f03f9b5edab08df193..5bb021ccb9b1bb9646c0035e587b450e0abe7e91 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\UserRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface; @@ -82,6 +84,16 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface */ private string $salt; + /** + * @ORM\ManyToMany(targetEntity=Capsule::class, mappedBy="editors") + */ + private $capsules; + + public function __construct() + { + $this->capsules = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -212,4 +224,31 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface { $this->salt = $salt; } + + /** + * @return Collection|Capsule[] + */ + public function getCapsules(): Collection + { + return $this->capsules; + } + + public function addCapsule(Capsule $capsule): self + { + if (!$this->capsules->contains($capsule)) { + $this->capsules[] = $capsule; + $capsule->addEditor($this); + } + + return $this; + } + + public function removeCapsule(Capsule $capsule): self + { + if ($this->capsules->removeElement($capsule)) { + $capsule->removeEditor($this); + } + + return $this; + } }