From 4e31f70bbcaaf6a5a1a8acdbb3f3f624abe67006 Mon Sep 17 00:00:00 2001 From: Julian PEREZ-RAMIREZ <julian.perez-ramirez@imt-atlantique.net> Date: Sun, 23 Feb 2025 17:24:41 +0100 Subject: [PATCH] creting v2 of db scheme --- web_app/migrations/Version20250223162147.php | 35 ++++++++++++++++++++ web_app/src/Entity/WishList.php | 35 ++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 web_app/migrations/Version20250223162147.php diff --git a/web_app/migrations/Version20250223162147.php b/web_app/migrations/Version20250223162147.php new file mode 100644 index 0000000..0ce2234 --- /dev/null +++ b/web_app/migrations/Version20250223162147.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 Version20250223162147 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 wishlist_item (wish_list_id INT NOT NULL, item_id INT NOT NULL, INDEX IDX_6424F4E8D69F3311 (wish_list_id), INDEX IDX_6424F4E8126F525E (item_id), PRIMARY KEY(wish_list_id, item_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE wishlist_item ADD CONSTRAINT FK_6424F4E8D69F3311 FOREIGN KEY (wish_list_id) REFERENCES wish_list (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE wishlist_item ADD CONSTRAINT FK_6424F4E8126F525E FOREIGN KEY (item_id) REFERENCES item (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 wishlist_item DROP FOREIGN KEY FK_6424F4E8D69F3311'); + $this->addSql('ALTER TABLE wishlist_item DROP FOREIGN KEY FK_6424F4E8126F525E'); + $this->addSql('DROP TABLE wishlist_item'); + } +} diff --git a/web_app/src/Entity/WishList.php b/web_app/src/Entity/WishList.php index 59041df..f8e4855 100644 --- a/web_app/src/Entity/WishList.php +++ b/web_app/src/Entity/WishList.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\WishListRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; @@ -21,6 +23,10 @@ class WishList #[ORM\Column(length: 255)] private ?string $name = null; + #[ORM\ManyToMany(targetEntity: Item::class)] + #[ORM\JoinTable(name: "wishlist_item")] + private Collection $items; + #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)] private ?\DateTimeInterface $expiration_date = null; @@ -30,6 +36,11 @@ class WishList #[ORM\Column] private ?\DateTimeImmutable $created_at = null; + public function __construct() + { + $this->items = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -94,4 +105,28 @@ class WishList return $this; } + + /** + * @return Collection<int, Item> + */ + public function getItems(): Collection + { + return $this->items; + } + + public function addItem(Item $item): static + { + if (!$this->items->contains($item)) { + $this->items->add($item); + } + + return $this; + } + + public function removeItem(Item $item): static + { + $this->items->removeElement($item); + + return $this; + } } -- GitLab