add menuitem aliases
This commit is contained in:
parent
70b39515ec
commit
9781bd561f
13 changed files with 713 additions and 212 deletions
|
@ -45,6 +45,11 @@ final class OrderItemController extends AbstractController
|
|||
$entityManager->persist($menuItem);
|
||||
}
|
||||
|
||||
if ($menuItem->getAliasOf() !== null) {
|
||||
$menuItem = $menuItem->getAliasOf();
|
||||
$orderItem->setName($menuItem->getName());
|
||||
}
|
||||
|
||||
$orderItem->setMenuItem($menuItem);
|
||||
$orderItem->setFoodOrder($foodOrder);
|
||||
$entityManager->persist($orderItem);
|
||||
|
|
50
src/DataFixtures/AppFixtures.php
Normal file
50
src/DataFixtures/AppFixtures.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\DataFixtures;
|
||||
|
||||
use App\Entity\FoodVendor;
|
||||
use App\Entity\MenuItem;
|
||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Override;
|
||||
|
||||
use function range;
|
||||
|
||||
final class AppFixtures extends Fixture
|
||||
{
|
||||
private ObjectManager $manager;
|
||||
|
||||
#[Override]
|
||||
public function load(ObjectManager $manager): void
|
||||
{
|
||||
$this->manager = $manager;
|
||||
$vendorA = $this->createVendor('Vendor A');
|
||||
$this->addMenuItemsToVendor($vendorA);
|
||||
|
||||
$vendorB = $this->createVendor('Vendor B');
|
||||
$this->addMenuItemsToVendor($vendorB);
|
||||
}
|
||||
|
||||
public function createVendor(string $name): FoodVendor
|
||||
{
|
||||
$vendorA = new FoodVendor;
|
||||
$vendorA->setName($name);
|
||||
$vendorA->setMenuLink('https://vendora.com');
|
||||
$vendorA->setPhone('1234567890');
|
||||
|
||||
$this->manager->persist($vendorA);
|
||||
$this->manager->flush();
|
||||
return $vendorA;
|
||||
}
|
||||
|
||||
public function addMenuItemsToVendor(FoodVendor $vendor): void
|
||||
{
|
||||
foreach (range(1, 10) as $i) {
|
||||
$item = new MenuItem;
|
||||
$item->setName("{$vendor->getName()} Item {$i}");
|
||||
$item->setFoodVendor($vendor);
|
||||
$this->manager->persist($item);
|
||||
$this->manager->flush();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,8 @@ namespace App\Entity;
|
|||
|
||||
use App\Repository\MenuItemRepository;
|
||||
use DateTimeImmutable;
|
||||
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;
|
||||
|
@ -22,13 +24,24 @@ class MenuItem
|
|||
#[ORM\Column(nullable: true)]
|
||||
private DateTimeImmutable|null $deletedAt = null;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'aliases')]
|
||||
private self|null $aliasOf = null;
|
||||
|
||||
/**
|
||||
* @var Collection<int, self>
|
||||
*/
|
||||
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'aliasOf')]
|
||||
private Collection $aliases;
|
||||
|
||||
public function __construct(
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
|
||||
#[ORM\Column(type: UlidType::NAME, unique: true)]
|
||||
#[ORM\CustomIdGenerator(class: UlidGenerator::class)]
|
||||
private Ulid|null $id = new Ulid
|
||||
) {}
|
||||
) {
|
||||
$this->aliases = new ArrayCollection;
|
||||
}
|
||||
|
||||
public function getId(): Ulid|null
|
||||
{
|
||||
|
@ -81,4 +94,44 @@ class MenuItem
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAliasOf(): self|null
|
||||
{
|
||||
return $this->aliasOf;
|
||||
}
|
||||
|
||||
public function setAliasOf(self|null $aliasOf): static
|
||||
{
|
||||
$this->aliasOf = $aliasOf;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, self>
|
||||
*/
|
||||
public function getAliases(): Collection
|
||||
{
|
||||
return $this->aliases;
|
||||
}
|
||||
|
||||
public function addAlias(self $alias): static
|
||||
{
|
||||
if (! $this->aliases->contains($alias)) {
|
||||
$this->aliases->add($alias);
|
||||
$alias->setAliasOf($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeAlias(self $alias): static
|
||||
{
|
||||
// set the owning side to null (unless already changed)
|
||||
if ($this->aliases->removeElement($alias) && $alias->getAliasOf() === $this) {
|
||||
$alias->setAliasOf(null);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,11 @@
|
|||
namespace App\Form;
|
||||
|
||||
use App\Entity\MenuItem;
|
||||
use App\Repository\MenuItemRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Override;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Bridge\Doctrine\Types\UlidType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
@ -13,8 +17,18 @@ final class MenuItemType extends AbstractType
|
|||
#[Override]
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$vendor = $options['data']->getFoodVendor();
|
||||
$vendorId = $vendor->getId();
|
||||
$builder
|
||||
->add('name')
|
||||
->add('aliasOf', EntityType::class, [
|
||||
'class' => MenuItem::class,
|
||||
'choice_label' => 'name',
|
||||
'query_builder' => static fn(MenuItemRepository $repository): QueryBuilder => $repository
|
||||
->createQueryBuilder('m')
|
||||
->where('m.foodVendor = :vendorId')
|
||||
->setParameter(':vendorId', $vendorId, UlidType::NAME),
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue