From ac248697ff9fd962871a351d81260d811f72d539 Mon Sep 17 00:00:00 2001 From: lubiana Date: Tue, 13 Feb 2024 21:43:30 +0100 Subject: [PATCH] rework entities --- migrations/Version20240212201659.php | 54 ------------------- migrations/Version20240212202919.php | 47 ----------------- src/Controller/FoodOrderController.php | 32 +++++++++-- src/DataFixtures/AppFixture.php | 67 +++++++++++++++++------- src/Entity/MenuItem.php | 4 ++ src/Entity/OrderItem.php | 16 ++++-- src/Form/OrderItemType.php | 10 ---- templates/base.html.twig | 1 + templates/food_order/orderitem.html.twig | 6 +++ templates/food_order/show.html.twig | 29 +++------- templates/start/index.html.twig | 2 +- 11 files changed, 110 insertions(+), 158 deletions(-) delete mode 100644 migrations/Version20240212201659.php delete mode 100644 migrations/Version20240212202919.php create mode 100644 templates/food_order/orderitem.html.twig diff --git a/migrations/Version20240212201659.php b/migrations/Version20240212201659.php deleted file mode 100644 index b57f6dc..0000000 --- a/migrations/Version20240212201659.php +++ /dev/null @@ -1,54 +0,0 @@ -addSql('CREATE TABLE food_order (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))'); - $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 UNIQUE INDEX UNIQ_52EA1F099AB44FE0 ON order_item (menu_item_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 menu_item'); - $this->addSql('DROP TABLE menu_item_alias'); - $this->addSql('DROP TABLE order_item'); - $this->addSql('DROP TABLE vendor'); - } -} diff --git a/migrations/Version20240212202919.php b/migrations/Version20240212202919.php deleted file mode 100644 index ffaee95..0000000 --- a/migrations/Version20240212202919.php +++ /dev/null @@ -1,47 +0,0 @@ -addSql('CREATE TEMPORARY TABLE __temp__food_order AS SELECT id, closed_at, started_by, started_at FROM food_order'); - $this->addSql('DROP TABLE food_order'); - $this->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('INSERT INTO food_order (id, closed_at, started_by, started_at) SELECT id, closed_at, started_by, started_at FROM __temp__food_order'); - $this->addSql('DROP TABLE __temp__food_order'); - $this->addSql('CREATE INDEX IDX_4485672F603EE73 ON food_order (vendor_id)'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('CREATE TEMPORARY TABLE __temp__food_order AS SELECT id, closed_at, started_by, started_at FROM food_order'); - $this->addSql('DROP TABLE food_order'); - $this->addSql('CREATE TABLE food_order (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))'); - $this->addSql('INSERT INTO food_order (id, closed_at, started_by, started_at) SELECT id, closed_at, started_by, started_at FROM __temp__food_order'); - $this->addSql('DROP TABLE __temp__food_order'); - } -} diff --git a/src/Controller/FoodOrderController.php b/src/Controller/FoodOrderController.php index 5507080..7ff4062 100644 --- a/src/Controller/FoodOrderController.php +++ b/src/Controller/FoodOrderController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Entity\FoodOrder; use App\Entity\MenuItem; use App\Entity\OrderItem; +use App\Form\OrderItemType; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -51,7 +52,7 @@ class FoodOrderController extends AbstractController } #[Route( - '/{foodOrder}/add', + '/{foodOrder}/add/{menuItem}', name: self::APP_FOODORDER_ADD_ITEM, methods: [Request::METHOD_GET, Request::METHOD_POST], )] @@ -59,8 +60,33 @@ class FoodOrderController extends AbstractController FoodOrder $foodOrder, MenuItem $menuItem, EntityManagerInterface $entityManager, + Request $request, ): Response { - return new Response('lol'); + $orderItem = new OrderItem(); + $orderItem->setFoodOrder($foodOrder); + $orderItem->setMenuItem($menuItem); + + $form = $this->createForm(OrderItemType::class, $orderItem); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($orderItem); + $entityManager->flush(); + return $this->redirectToRoute( + FoodOrderController::APP_FOOD_ORDER_SHOW, + [ + 'id' => $foodOrder->getId(), + ], + Response::HTTP_SEE_OTHER, + ); + } + + return $this->render( + 'food_order/orderitem.html.twig', + [ + 'form' => $form, + ], + ); + } #[Route( @@ -78,7 +104,7 @@ class FoodOrderController extends AbstractController return $this->redirectToRoute( self::APP_FOOD_ORDER_SHOW, [ - 'id' => $foodOrder->id, + 'id' => $foodOrder->getId(), ], Response::HTTP_SEE_OTHER, ); diff --git a/src/DataFixtures/AppFixture.php b/src/DataFixtures/AppFixture.php index 02603fa..c8554d1 100644 --- a/src/DataFixtures/AppFixture.php +++ b/src/DataFixtures/AppFixture.php @@ -16,27 +16,58 @@ final class AppFixture extends Fixture { $this->faker = \Faker\Factory::create(); foreach (range(0, 20) as $vendorCount) { - $vendor = new Vendor(); - $vendor->setName($this->faker->name); - $manager->persist($vendor); - - foreach (range(0, 10) as $itemCount) { - $menuItem = new MenuItem(); - $menuItem->setVendor($vendor); - $menuItem->setPrice(random_int(500, 2000)); - $manager->persist($menuItem); - $menuItemAliasOne = new MenuItemAlias(); - $menuItemAliasOne->setName($this->faker->word); - $menuItem->addMenuItemAlias($menuItemAliasOne); - $manager->persist($menuItemAliasOne); - $menuItemAliasTwo = new MenuItemAlias(); - $menuItemAliasTwo->setName($this->faker->word); - $menuItem->addMenuItemAlias($menuItemAliasTwo); - $manager->persist($menuItemAliasTwo); - } + $this->createVendorAndMenuItems($manager); } $manager->flush(); } + + /** + * @param MenuItem $menuItem + * @param ObjectManager $manager + * @return void + */ + public function addMenuItemAliases(MenuItem $menuItem, ObjectManager $manager): void + { + $menuItemAliasOne = new MenuItemAlias(); + $menuItemAliasOne->setName($this->faker->word); + $menuItem->addMenuItemAlias($menuItemAliasOne); + $manager->persist($menuItemAliasOne); + $menuItemAliasTwo = new MenuItemAlias(); + $menuItemAliasTwo->setName($this->faker->word); + $menuItem->addMenuItemAlias($menuItemAliasTwo); + $manager->persist($menuItemAliasTwo); + } + + /** + * @param Vendor $vendor + * @param ObjectManager $manager + * @return void + * @throws \Random\RandomException + */ + public function createMenuItem(Vendor $vendor, ObjectManager $manager): void + { + $menuItem = new MenuItem(); + $menuItem->setVendor($vendor); + $menuItem->setPrice(random_int(500, 2000)); + $manager->persist($menuItem); + $this->addMenuItemAliases($menuItem, $manager); + } + + /** + * @param ObjectManager $manager + * @return void + * @throws \Random\RandomException + */ + public function createVendorAndMenuItems(ObjectManager $manager): void + { + $vendor = new Vendor(); + $vendor->setName($this->faker->name); + $manager->persist($vendor); + + foreach (range(0, 10) as $itemCount) { + $this->createMenuItem($vendor, $manager); + } + } } \ No newline at end of file diff --git a/src/Entity/MenuItem.php b/src/Entity/MenuItem.php index 584259f..45e166f 100644 --- a/src/Entity/MenuItem.php +++ b/src/Entity/MenuItem.php @@ -33,10 +33,14 @@ class MenuItem )] private Collection $menuItemAliases; + #[ORM\OneToMany(mappedBy: 'menuItem', targetEntity: ItemExtra::class, orphanRemoval: true)] + private Collection $itemExtras; + public function __construct() { $this->id = new Ulid; $this->menuItemAliases = new ArrayCollection; + $this->itemExtras = new ArrayCollection(); } public function getId(): Ulid diff --git a/src/Entity/OrderItem.php b/src/Entity/OrderItem.php index 464124d..9d74a7a 100644 --- a/src/Entity/OrderItem.php +++ b/src/Entity/OrderItem.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\OrderItemRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator; use Symfony\Bridge\Doctrine\Types\UlidType; @@ -21,13 +23,17 @@ class OrderItem #[ORM\JoinColumn(nullable: false)] private FoodOrder|null $foodOrder = null; - #[ORM\OneToOne(cascade: ['persist', 'remove'])] + #[ORM\ManyToOne] #[ORM\JoinColumn(nullable: false)] - private MenuItem|null $menuItem = null; + private ?MenuItem $menuItem = null; + + #[ORM\OneToMany(mappedBy: 'orderItem', targetEntity: ItemExtra::class)] + private Collection $extras; public function __construct() { $this->id = new Ulid; + $this->extras = new ArrayCollection(); } public function getId(): Ulid @@ -46,14 +52,16 @@ class OrderItem return $this; } - public function getMenuItem(): MenuItem|null + public function getMenuItem(): ?MenuItem { return $this->menuItem; } - public function setMenuItem(MenuItem $menuItem): static + public function setMenuItem(?MenuItem $menuItem): static { $this->menuItem = $menuItem; + return $this; } + } diff --git a/src/Form/OrderItemType.php b/src/Form/OrderItemType.php index 2e3288f..3246494 100644 --- a/src/Form/OrderItemType.php +++ b/src/Form/OrderItemType.php @@ -14,16 +14,6 @@ class OrderItemType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { - $builder - ->add('foodOrder', EntityType::class, [ - 'class' => FoodOrder::class, -'choice_label' => 'id', - ]) - ->add('menuItem', EntityType::class, [ - 'class' => MenuItem::class, -'choice_label' => 'id', - ]) - ; } public function configureOptions(OptionsResolver $resolver): void diff --git a/templates/base.html.twig b/templates/base.html.twig index 57a74af..b989963 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -18,6 +18,7 @@ Henlo {{ username }} (change name) | index

+
{% block body %}{% endblock %} diff --git a/templates/food_order/orderitem.html.twig b/templates/food_order/orderitem.html.twig new file mode 100644 index 0000000..a647b6b --- /dev/null +++ b/templates/food_order/orderitem.html.twig @@ -0,0 +1,6 @@ +{% extends 'base.html.twig' %} + +{% block body %} + {{ include('_form.html.twig') }} +{% endblock %} + diff --git a/templates/food_order/show.html.twig b/templates/food_order/show.html.twig index 4dadfaf..1b91d53 100644 --- a/templates/food_order/show.html.twig +++ b/templates/food_order/show.html.twig @@ -44,7 +44,7 @@ - {{ lel.item.menuItem.aliases|map(i => i.name)|join(' / ') }} + {{ lel.item.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }} {{ lel.item.menuItem.price|cents_to_eur }} @@ -58,7 +58,6 @@ {% endfor %} - @@ -79,29 +78,17 @@ {% endfor %}
{% if food_order.closedAt is null %} add {% endif %}
- {% endblock %} diff --git a/templates/start/index.html.twig b/templates/start/index.html.twig index 269ed2d..df5e458 100644 --- a/templates/start/index.html.twig +++ b/templates/start/index.html.twig @@ -16,7 +16,7 @@ {% for order in orders %} - {{ order.startedByName }} + {{ order.startedBy }} {{ order.vendor.name }} {{ order.startedAt|date("Y-m-d H:i") }} show