From c92e561b750d5c3f4a6ce5b237cc41d368250238 Mon Sep 17 00:00:00 2001 From: Camille Simiand <camille.simiand@tetras-libre.fr> Date: Wed, 15 Dec 2021 15:38:20 +0100 Subject: [PATCH] Update capsule table schema --- migrations/Version20211215142553.php | 37 ------------------------- migrations/Version20211215152200.php | 31 +++++++++++++++++++++ src/DataFixtures/AppCapsuleFixtures.php | 26 ++++++++--------- src/Entity/User.php | 9 ++++-- 4 files changed, 50 insertions(+), 53 deletions(-) delete mode 100644 migrations/Version20211215142553.php create mode 100644 migrations/Version20211215152200.php diff --git a/migrations/Version20211215142553.php b/migrations/Version20211215142553.php deleted file mode 100644 index 548a22b..0000000 --- a/migrations/Version20211215142553.php +++ /dev/null @@ -1,37 +0,0 @@ -<?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 Version20211215142553 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 INT NOT NULL, dt_crea DATETIME NOT NULL, aut_maj INT DEFAULT 0 NOT NULL, dt_maj DATETIME DEFAULT \'0\' 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('ALTER TABLE editeur_capsule DROP FOREIGN KEY FK_A18592E2A76ED395'); - $this->addSql('DROP TABLE capsule'); - $this->addSql('DROP TABLE editeur_capsule'); - } -} diff --git a/migrations/Version20211215152200.php b/migrations/Version20211215152200.php new file mode 100644 index 0000000..be6f5e4 --- /dev/null +++ b/migrations/Version20211215152200.php @@ -0,0 +1,31 @@ +<?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 Version20211215152200 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 INT DEFAULT NULL, dt_maj DATETIME DEFAULT NULL, link VARCHAR(255) NOT NULL, edition_link VARCHAR(255) NOT NULL, UNIQUE INDEX index_capsule_nom (nom), UNIQUE INDEX index_capsule_link (link), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE capsule'); + } +} diff --git a/src/DataFixtures/AppCapsuleFixtures.php b/src/DataFixtures/AppCapsuleFixtures.php index fa0cde8..02163fb 100644 --- a/src/DataFixtures/AppCapsuleFixtures.php +++ b/src/DataFixtures/AppCapsuleFixtures.php @@ -13,24 +13,24 @@ class AppCapsuleFixtures extends Fixture { $this->createCapsuleWithData( $manager, - 'anonymous', + 2, new DateTime(), 'https://youtu.be/oPmMUZbnc3o', 'Adele', '', - '', - new DateTime() + null, + null ); $this->createCapsuleWithData( $manager, - 'Camille', + 2, new DateTime(), 'https://youtu.be/-xulsY5AGew', 'Pomme', '', - '', - new DateTime() + null, + null ); $manager->flush(); @@ -38,22 +38,22 @@ class AppCapsuleFixtures extends Fixture private function createCapsuleWithData( ObjectManager $manager, - string $author, + int $author, DateTime $date_time, string $link, string $name, string $edition_link, - string $update_author, + ?int $update_author, ?DateTime $update_datetime ): void { $capsule = new Capsule(); - $capsule->setAuthor($author); - $capsule->setCreationDate($date_time); + $capsule->setAutCrea($author); + $capsule->setDtCrea($date_time); $capsule->setLink($link); $capsule->setEditionLink($edition_link); - $capsule->setName($name); - $capsule->setUpdateAuthor($update_author); - $capsule->setUpdateDate($update_datetime); + $capsule->setNom($name); + $capsule->setAutMaj($update_author); + $capsule->setDtMaj($update_datetime); $manager->persist($capsule); } diff --git a/src/Entity/User.php b/src/Entity/User.php index 5bb021c..37a58c6 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -85,7 +85,7 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface private string $salt; /** - * @ORM\ManyToMany(targetEntity=Capsule::class, mappedBy="editors") + * @ORM\OneToMany(targetEntity=Capsule::class, mappedBy="aut_crea") */ private $capsules; @@ -237,7 +237,7 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface { if (!$this->capsules->contains($capsule)) { $this->capsules[] = $capsule; - $capsule->addEditor($this); + $capsule->setAutCrea($this); } return $this; @@ -246,7 +246,10 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface public function removeCapsule(Capsule $capsule): self { if ($this->capsules->removeElement($capsule)) { - $capsule->removeEditor($this); + // set the owning side to null (unless already changed) + if ($capsule->getAutCrea() === $this) { + $capsule->setAutCrea(null); + } } return $this; -- GitLab