add option to select previous menuitems
This commit is contained in:
parent
57b4e33028
commit
45029e0a4c
11 changed files with 410 additions and 94 deletions
66
src/Command/MigrateOrderitemsMenuitemsCommand.php
Normal file
66
src/Command/MigrateOrderitemsMenuitemsCommand.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\MenuItem;
|
||||
use App\Repository\MenuItemRepository;
|
||||
use App\Repository\OrderItemRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Override;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:migrate-orderitems-menuitems',
|
||||
description: 'Migrate orderitems to menu items',
|
||||
)]
|
||||
final class MigrateOrderitemsMenuitemsCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly OrderItemRepository $orderItemRepository,
|
||||
private readonly MenuItemRepository $menuItemRepository,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[Override]
|
||||
protected function configure(): void {}
|
||||
|
||||
#[Override]
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$orderItems = $this->orderItemRepository->findAll();
|
||||
|
||||
foreach ($orderItems as $orderItem) {
|
||||
|
||||
$menuItem = $this->menuItemRepository->findOneBy([
|
||||
'name' => $orderItem->getName(),
|
||||
'foodVendor' => $orderItem->getFoodOrder()
|
||||
->getFoodVendor(),
|
||||
]);
|
||||
|
||||
if ($menuItem === null) {
|
||||
$menuItem = new MenuItem;
|
||||
$menuItem->setName($orderItem->getName());
|
||||
$menuItem->setFoodVendor($orderItem->getFoodOrder()->getFoodVendor());
|
||||
$this->entityManager->persist($menuItem);
|
||||
$this->entityManager->flush();
|
||||
$output->writeln(sprintf('Menu item %s added', $menuItem->getName()));
|
||||
}
|
||||
$orderItem->setMenuItem($menuItem);
|
||||
$this->entityManager->persist($orderItem);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
|
@ -3,8 +3,10 @@
|
|||
namespace App\Controller;
|
||||
|
||||
use App\Entity\FoodOrder;
|
||||
use App\Entity\MenuItem;
|
||||
use App\Entity\OrderItem;
|
||||
use App\Form\OrderItemType;
|
||||
use App\Repository\MenuItemRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
@ -15,18 +17,32 @@ use Symfony\Component\Routing\Attribute\Route;
|
|||
final class OrderItemController extends AbstractController
|
||||
{
|
||||
#[Route('/new/{foodOrder}', name: 'app_order_item_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, FoodOrder $foodOrder, EntityManagerInterface $entityManager): Response
|
||||
public function new(Request $request, FoodOrder $foodOrder, EntityManagerInterface $entityManager, MenuItemRepository $menuItemRepository): Response
|
||||
{
|
||||
if ($foodOrder->isClosed()) {
|
||||
return $this->redirectToRoute('app_food_order_show', [
|
||||
'id' => $foodOrder->getId(),
|
||||
], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
$orderItem = new OrderItem;
|
||||
$form = $this->createForm(OrderItemType::class, $orderItem);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$menuItem = $menuItemRepository->findOneBy([
|
||||
'name' => $orderItem->getName(),
|
||||
'foodVendor' => $foodOrder->getFoodVendor(),
|
||||
]);
|
||||
|
||||
if ($menuItem === null) {
|
||||
$menuItem = new MenuItem;
|
||||
$menuItem->setName($orderItem->getName());
|
||||
$menuItem->setFoodVendor($foodOrder->getFoodVendor());
|
||||
$entityManager->persist($menuItem);
|
||||
}
|
||||
|
||||
$orderItem->setMenuItem($menuItem);
|
||||
$orderItem->setFoodOrder($foodOrder);
|
||||
$entityManager->persist($orderItem);
|
||||
$entityManager->flush();
|
||||
|
@ -35,11 +51,15 @@ final class OrderItemController extends AbstractController
|
|||
'id' => $foodOrder->getId(),
|
||||
], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
$menuItems = $menuItemRepository->findBy([
|
||||
'foodVendor' => $foodOrder->getFoodVendor(),
|
||||
]);
|
||||
|
||||
return $this->render('order_item/new.html.twig', [
|
||||
'order_item' => $orderItem,
|
||||
'food_order' => $foodOrder,
|
||||
'form' => $form,
|
||||
'menuItems' => $menuItems,
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -28,9 +28,16 @@ class FoodVendor
|
|||
#[ORM\OneToMany(targetEntity: FoodOrder::class, mappedBy: 'foodVendor', orphanRemoval: true)]
|
||||
private Collection $foodOrders;
|
||||
|
||||
/**
|
||||
* @var Collection<int, MenuItem>
|
||||
*/
|
||||
#[ORM\OneToMany(targetEntity: MenuItem::class, mappedBy: 'foodVendor', orphanRemoval: true)]
|
||||
private Collection $menuItems;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->foodOrders = new ArrayCollection;
|
||||
$this->menuItems = new ArrayCollection;
|
||||
}
|
||||
|
||||
public function getId(): Ulid|null
|
||||
|
@ -77,4 +84,32 @@ class FoodVendor
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, MenuItem>
|
||||
*/
|
||||
public function getMenuItems(): Collection
|
||||
{
|
||||
return $this->menuItems;
|
||||
}
|
||||
|
||||
public function addMenuItem(MenuItem $menuItem): static
|
||||
{
|
||||
if (! $this->menuItems->contains($menuItem)) {
|
||||
$this->menuItems->add($menuItem);
|
||||
$menuItem->setFoodVendor($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeMenuItem(MenuItem $menuItem): static
|
||||
{
|
||||
// set the owning side to null (unless already changed)
|
||||
if ($this->menuItems->removeElement($menuItem) && $menuItem->getFoodVendor() === $this) {
|
||||
$menuItem->setFoodVendor(null);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
55
src/Entity/MenuItem.php
Normal file
55
src/Entity/MenuItem.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\MenuItemRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
|
||||
use Symfony\Bridge\Doctrine\Types\UlidType;
|
||||
use Symfony\Component\Uid\Ulid;
|
||||
|
||||
#[ORM\Entity(repositoryClass: MenuItemRepository::class)]
|
||||
class MenuItem
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
|
||||
#[ORM\Column(type: UlidType::NAME, unique: true)]
|
||||
#[ORM\CustomIdGenerator(class: UlidGenerator::class)]
|
||||
private Ulid|null $id = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private string|null $name = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'menuItems')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private FoodVendor|null $foodVendor = null;
|
||||
|
||||
public function getId(): Ulid|null
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): string|null
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFoodVendor(): FoodVendor|null
|
||||
{
|
||||
return $this->foodVendor;
|
||||
}
|
||||
|
||||
public function setFoodVendor(FoodVendor|null $foodVendor): static
|
||||
{
|
||||
$this->foodVendor = $foodVendor;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -24,9 +24,13 @@ class OrderItem
|
|||
private string|null $extras = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'orderItems')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
#[ORM\JoinColumn(nullable: true)]
|
||||
private FoodOrder|null $foodOrder = null;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(nullable: true)]
|
||||
private MenuItem|null $menuItem = null;
|
||||
|
||||
public function getId(): Ulid|null
|
||||
{
|
||||
return $this->id;
|
||||
|
@ -67,4 +71,16 @@ class OrderItem
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMenuItem(): MenuItem|null
|
||||
{
|
||||
return $this->menuItem;
|
||||
}
|
||||
|
||||
public function setMenuItem(MenuItem|null $menuItem): static
|
||||
{
|
||||
$this->menuItem = $menuItem;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
43
src/Repository/MenuItemRepository.php
Normal file
43
src/Repository/MenuItemRepository.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\MenuItem;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<MenuItem>
|
||||
*/
|
||||
final class MenuItemRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, MenuItem::class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return MenuItem[] Returns an array of MenuItem objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('m')
|
||||
// ->andWhere('m.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('m.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?MenuItem
|
||||
// {
|
||||
// return $this->createQueryBuilder('m')
|
||||
// ->andWhere('m.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue