add item extras

This commit is contained in:
Jonas 2024-02-13 22:48:56 +01:00
parent 1dab51005a
commit 1a99758a0c
6 changed files with 53 additions and 15 deletions

View file

@ -66,7 +66,7 @@ class FoodOrderController extends AbstractController
$orderItem->setFoodOrder($foodOrder); $orderItem->setFoodOrder($foodOrder);
$orderItem->setMenuItem($menuItem); $orderItem->setMenuItem($menuItem);
$form = $this->createForm(OrderItemType::class, $orderItem); $form = $this->createForm(OrderItemType::class, $orderItem, ['menuItem' => $menuItem]);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($orderItem); $entityManager->persist($orderItem);

View file

@ -2,6 +2,7 @@
namespace App\DataFixtures; namespace App\DataFixtures;
use App\Entity\ItemExtra;
use App\Entity\MenuItem; use App\Entity\MenuItem;
use App\Entity\MenuItemAlias; use App\Entity\MenuItemAlias;
use App\Entity\Vendor; use App\Entity\Vendor;
@ -17,7 +18,6 @@ final class AppFixture extends Fixture
$this->faker = \Faker\Factory::create(); $this->faker = \Faker\Factory::create();
foreach (range(0, 20) as $vendorCount) { foreach (range(0, 20) as $vendorCount) {
$this->createVendorAndMenuItems($manager); $this->createVendorAndMenuItems($manager);
} }
$manager->flush(); $manager->flush();
@ -40,6 +40,23 @@ final class AppFixture extends Fixture
$manager->persist($menuItemAliasTwo); $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 Vendor $vendor
* @param ObjectManager $manager * @param ObjectManager $manager
@ -53,6 +70,7 @@ final class AppFixture extends Fixture
$menuItem->setPrice(random_int(500, 2000)); $menuItem->setPrice(random_int(500, 2000));
$manager->persist($menuItem); $manager->persist($menuItem);
$this->addMenuItemAliases($menuItem, $manager); $this->addMenuItemAliases($menuItem, $manager);
$this->addMenuItemExtras($menuItem, $manager);
} }
/** /**

View file

@ -27,7 +27,7 @@ class OrderItem
#[ORM\JoinColumn(nullable: false)] #[ORM\JoinColumn(nullable: false)]
private ?MenuItem $menuItem = null; private ?MenuItem $menuItem = null;
#[ORM\ManyToMany(targetEntity: ItemExtra::class)] #[ORM\ManyToMany(targetEntity: ItemExtra::class, cascade: ['persist'])]
private Collection $extras; private Collection $extras;
public function __construct() public function __construct()

View file

@ -33,8 +33,7 @@ class FoodOrderType extends AbstractType
'choice_label' => 'name', 'choice_label' => 'name',
'multiple' => false, 'multiple' => false,
], ],
) );
->add('startedAt');
} }
#[Override] #[Override]

View file

@ -2,24 +2,42 @@
namespace App\Form; namespace App\Form;
use App\Entity\FoodOrder;
use App\Entity\MenuItem; use App\Entity\MenuItem;
use App\Entity\ItemExtra;
use App\Entity\OrderItem; use App\Entity\OrderItem;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
// ...
class OrderItemType extends AbstractType 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([ $resolver->setDefaults([
'data_class' => OrderItem::class, 'data_class' => OrderItem::class,
]); ]);
$resolver->setRequired('menuItem');
$resolver->setAllowedTypes('menuItem', MenuItem::class);
} }
} }

View file

@ -35,24 +35,27 @@
<thead> <thead>
<tr> <tr>
<th>Name(s)</th> <th>Name(s)</th>
<th>Extra(s)</th>
<th>possible price</th> <th>possible price</th>
<th>actions</th> <th>actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for lel in food_order.groupedOrderItems %} {% for orderItem in food_order.groupedOrderItems %}
<tr> <tr>
<td> <td>
{{ orderItem.item.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }}
{{ lel.item.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }}
</td> </td>
<td> <td>
{{ lel.item.menuItem.price|cents_to_eur }} {{ orderItem.item.extras|map(i => i.name)|join(' / ') }}
</td> </td>
<td> <td>
{{ lel.amount }} | {{ orderItem.item.menuItem.price|cents_to_eur }}
<a href="{{ path('app_foodorder_add_item', {foodOrder: food_order.id, menuItem: lel.item.menuItem.id}) }}">+1</a> | </td>
<a href="{{ path('app_foodorder_remove_item', {foodOrder: food_order.id, orderItem: lel.item.id}) }}">-1</a> | <td>
{{ orderItem.amount }} |
<a href="{{ path('app_foodorder_add_item', {foodOrder: food_order.id, menuItem: orderItem.item.menuItem.id}) }}">+1</a> |
<a href="{{ path('app_foodorder_remove_item', {foodOrder: food_order.id, orderItem: orderItem.item.id}) }}">-1</a> |
</td> </td>
</tr> </tr>