add item extras
This commit is contained in:
parent
1dab51005a
commit
1a99758a0c
6 changed files with 53 additions and 15 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -33,8 +33,7 @@ class FoodOrderType extends AbstractType
|
|||
'choice_label' => 'name',
|
||||
'multiple' => false,
|
||||
],
|
||||
)
|
||||
->add('startedAt');
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,24 +35,27 @@
|
|||
<thead>
|
||||
<tr>
|
||||
<th>Name(s)</th>
|
||||
<th>Extra(s)</th>
|
||||
<th>possible price</th>
|
||||
<th>actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for lel in food_order.groupedOrderItems %}
|
||||
{% for orderItem in food_order.groupedOrderItems %}
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
{{ lel.item.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }}
|
||||
{{ orderItem.item.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }}
|
||||
</td>
|
||||
<td>
|
||||
{{ lel.item.menuItem.price|cents_to_eur }}
|
||||
{{ orderItem.item.extras|map(i => i.name)|join(' / ') }}
|
||||
</td>
|
||||
<td>
|
||||
{{ lel.amount }} |
|
||||
<a href="{{ path('app_foodorder_add_item', {foodOrder: food_order.id, menuItem: lel.item.menuItem.id}) }}">+1</a> |
|
||||
<a href="{{ path('app_foodorder_remove_item', {foodOrder: food_order.id, orderItem: lel.item.id}) }}">-1</a> |
|
||||
{{ orderItem.item.menuItem.price|cents_to_eur }}
|
||||
</td>
|
||||
<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>
|
||||
</tr>
|
||||
|
|
Loading…
Reference in a new issue