From 1dab51005aec60979ab6b41a61f528540f33a5ba Mon Sep 17 00:00:00 2001 From: lubiana Date: Tue, 13 Feb 2024 21:50:10 +0100 Subject: [PATCH] add itemextras to to orderitem --- migrations/Version20240213204922.php | 67 ++++++++++++++++++++++++++++ src/Entity/OrderItem.php | 27 +++++++++++ 2 files changed, 94 insertions(+) create mode 100644 migrations/Version20240213204922.php diff --git a/migrations/Version20240213204922.php b/migrations/Version20240213204922.php new file mode 100644 index 0000000..efce817 --- /dev/null +++ b/migrations/Version20240213204922.php @@ -0,0 +1,67 @@ +addSql('CREATE TABLE food_order (id BLOB NOT NULL --(DC2Type:ulid) + , vendor_id BLOB NOT NULL --(DC2Type:ulid) + , closed_at DATETIME DEFAULT NULL --(DC2Type:datetime_immutable) + , started_by VARCHAR(30) NOT NULL, started_at DATETIME NOT NULL --(DC2Type:datetime_immutable) + , PRIMARY KEY(id), CONSTRAINT FK_4485672F603EE73 FOREIGN KEY (vendor_id) REFERENCES vendor (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_4485672F603EE73 ON food_order (vendor_id)'); + $this->addSql('CREATE TABLE item_extra (id BLOB NOT NULL --(DC2Type:ulid) + , menu_item_id BLOB NOT NULL --(DC2Type:ulid) + , name VARCHAR(50) NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_679816B59AB44FE0 FOREIGN KEY (menu_item_id) REFERENCES menu_item (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_679816B59AB44FE0 ON item_extra (menu_item_id)'); + $this->addSql('CREATE TABLE menu_item (id BLOB NOT NULL --(DC2Type:ulid) + , vendor_id BLOB NOT NULL --(DC2Type:ulid) + , price INTEGER NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_D754D550F603EE73 FOREIGN KEY (vendor_id) REFERENCES vendor (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_D754D550F603EE73 ON menu_item (vendor_id)'); + $this->addSql('CREATE TABLE menu_item_alias (id BLOB NOT NULL --(DC2Type:ulid) + , menu_item_id BLOB NOT NULL --(DC2Type:ulid) + , name VARCHAR(50) NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_EA66C4969AB44FE0 FOREIGN KEY (menu_item_id) REFERENCES menu_item (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_EA66C4969AB44FE0 ON menu_item_alias (menu_item_id)'); + $this->addSql('CREATE TABLE order_item (id BLOB NOT NULL --(DC2Type:ulid) + , food_order_id BLOB NOT NULL --(DC2Type:ulid) + , menu_item_id BLOB NOT NULL --(DC2Type:ulid) + , PRIMARY KEY(id), CONSTRAINT FK_52EA1F09A5D24A7A FOREIGN KEY (food_order_id) REFERENCES food_order (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_52EA1F099AB44FE0 FOREIGN KEY (menu_item_id) REFERENCES menu_item (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_52EA1F09A5D24A7A ON order_item (food_order_id)'); + $this->addSql('CREATE INDEX IDX_52EA1F099AB44FE0 ON order_item (menu_item_id)'); + $this->addSql('CREATE TABLE order_item_item_extra (order_item_id BLOB NOT NULL --(DC2Type:ulid) + , item_extra_id BLOB NOT NULL --(DC2Type:ulid) + , PRIMARY KEY(order_item_id, item_extra_id), CONSTRAINT FK_1120FDE3E415FB15 FOREIGN KEY (order_item_id) REFERENCES order_item (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_1120FDE34798570D FOREIGN KEY (item_extra_id) REFERENCES item_extra (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_1120FDE3E415FB15 ON order_item_item_extra (order_item_id)'); + $this->addSql('CREATE INDEX IDX_1120FDE34798570D ON order_item_item_extra (item_extra_id)'); + $this->addSql('CREATE TABLE vendor (id BLOB NOT NULL --(DC2Type:ulid) + , name VARCHAR(50) NOT NULL, PRIMARY KEY(id))'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE food_order'); + $this->addSql('DROP TABLE item_extra'); + $this->addSql('DROP TABLE menu_item'); + $this->addSql('DROP TABLE menu_item_alias'); + $this->addSql('DROP TABLE order_item'); + $this->addSql('DROP TABLE order_item_item_extra'); + $this->addSql('DROP TABLE vendor'); + } +} diff --git a/src/Entity/OrderItem.php b/src/Entity/OrderItem.php index b0c4f2b..5dd1533 100644 --- a/src/Entity/OrderItem.php +++ b/src/Entity/OrderItem.php @@ -27,6 +27,9 @@ class OrderItem #[ORM\JoinColumn(nullable: false)] private ?MenuItem $menuItem = null; + #[ORM\ManyToMany(targetEntity: ItemExtra::class)] + private Collection $extras; + public function __construct() { $this->id = new Ulid; @@ -61,4 +64,28 @@ class OrderItem return $this; } + /** + * @return Collection + */ + public function getExtras(): Collection + { + return $this->extras; + } + + public function addExtra(ItemExtra $extra): static + { + if (!$this->extras->contains($extra)) { + $this->extras->add($extra); + } + + return $this; + } + + public function removeExtra(ItemExtra $extra): static + { + $this->extras->removeElement($extra); + + return $this; + } + }