From 1a99758a0caa957f60113062942dd446cc0f82e3 Mon Sep 17 00:00:00 2001 From: lubiana Date: Tue, 13 Feb 2024 22:48:56 +0100 Subject: [PATCH] add item extras --- src/Controller/FoodOrderController.php | 2 +- src/DataFixtures/AppFixture.php | 20 +++++++++++++++++++- src/Entity/OrderItem.php | 2 +- src/Form/FoodOrderType.php | 3 +-- src/Form/OrderItemType.php | 24 +++++++++++++++++++++--- templates/food_order/show.html.twig | 17 ++++++++++------- 6 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/Controller/FoodOrderController.php b/src/Controller/FoodOrderController.php index 7ff4062..3b3432f 100644 --- a/src/Controller/FoodOrderController.php +++ b/src/Controller/FoodOrderController.php @@ -66,7 +66,7 @@ class FoodOrderController extends AbstractController $orderItem->setFoodOrder($foodOrder); $orderItem->setMenuItem($menuItem); - $form = $this->createForm(OrderItemType::class, $orderItem); + $form = $this->createForm(OrderItemType::class, $orderItem, ['menuItem' => $menuItem]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $entityManager->persist($orderItem); diff --git a/src/DataFixtures/AppFixture.php b/src/DataFixtures/AppFixture.php index c8554d1..0625204 100644 --- a/src/DataFixtures/AppFixture.php +++ b/src/DataFixtures/AppFixture.php @@ -2,6 +2,7 @@ namespace App\DataFixtures; +use App\Entity\ItemExtra; use App\Entity\MenuItem; use App\Entity\MenuItemAlias; use App\Entity\Vendor; @@ -17,7 +18,6 @@ final class AppFixture extends Fixture $this->faker = \Faker\Factory::create(); foreach (range(0, 20) as $vendorCount) { $this->createVendorAndMenuItems($manager); - } $manager->flush(); @@ -40,6 +40,23 @@ final class AppFixture extends Fixture $manager->persist($menuItemAliasTwo); } + /** + * @param MenuItem $menuItem + * @param ObjectManager $manager + * @return void + * @throws \Random\RandomException + */ + public function addMenuItemExtras(MenuItem $menuItem, ObjectManager $manager): void + { + + foreach(range(0, random_int(0, 5)) as $dispose) { + $itemExtra = new ItemExtra(); + $itemExtra->setName($this->faker->word); + $menuItem->addItemExtra($itemExtra); + $manager->persist($itemExtra); + } + } + /** * @param Vendor $vendor * @param ObjectManager $manager @@ -53,6 +70,7 @@ final class AppFixture extends Fixture $menuItem->setPrice(random_int(500, 2000)); $manager->persist($menuItem); $this->addMenuItemAliases($menuItem, $manager); + $this->addMenuItemExtras($menuItem, $manager); } /** diff --git a/src/Entity/OrderItem.php b/src/Entity/OrderItem.php index 5dd1533..47a87db 100644 --- a/src/Entity/OrderItem.php +++ b/src/Entity/OrderItem.php @@ -27,7 +27,7 @@ class OrderItem #[ORM\JoinColumn(nullable: false)] private ?MenuItem $menuItem = null; - #[ORM\ManyToMany(targetEntity: ItemExtra::class)] + #[ORM\ManyToMany(targetEntity: ItemExtra::class, cascade: ['persist'])] private Collection $extras; public function __construct() diff --git a/src/Form/FoodOrderType.php b/src/Form/FoodOrderType.php index 4f4af00..0f54812 100644 --- a/src/Form/FoodOrderType.php +++ b/src/Form/FoodOrderType.php @@ -33,8 +33,7 @@ class FoodOrderType extends AbstractType 'choice_label' => 'name', 'multiple' => false, ], - ) - ->add('startedAt'); + ); } #[Override] diff --git a/src/Form/OrderItemType.php b/src/Form/OrderItemType.php index 3246494..e080dfd 100644 --- a/src/Form/OrderItemType.php +++ b/src/Form/OrderItemType.php @@ -2,24 +2,42 @@ namespace App\Form; -use App\Entity\FoodOrder; use App\Entity\MenuItem; +use App\Entity\ItemExtra; use App\Entity\OrderItem; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +// ... + class OrderItemType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options): void + /** + * @param FormBuilderInterface $builder + * @param array{menuItem: MenuItem} $options + * @return void + */ + public function buildForm(FormBuilderInterface $builder, array $options) { + $builder + ->add('extras', EntityType::class, [ + 'class' => ItemExtra::class, + 'choices' => $options['menuItem']->getItemExtras(), + 'choice_label' => 'name', // Change this for the field you want to display + 'multiple' => true, + 'expanded' => true + ]); } - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => OrderItem::class, ]); + + $resolver->setRequired('menuItem'); + $resolver->setAllowedTypes('menuItem', MenuItem::class); } } diff --git a/templates/food_order/show.html.twig b/templates/food_order/show.html.twig index 1b91d53..3ac9853 100644 --- a/templates/food_order/show.html.twig +++ b/templates/food_order/show.html.twig @@ -35,24 +35,27 @@ Name(s) + Extra(s) possible price actions - {% for lel in food_order.groupedOrderItems %} + {% for orderItem in food_order.groupedOrderItems %} - - {{ lel.item.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }} + {{ orderItem.item.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }} - {{ lel.item.menuItem.price|cents_to_eur }} + {{ orderItem.item.extras|map(i => i.name)|join(' / ') }} - {{ lel.amount }} | - +1 | - -1 | + {{ orderItem.item.menuItem.price|cents_to_eur }} + + + {{ orderItem.amount }} | + +1 | + -1 |