make aliases work
All checks were successful
/ ls (pull_request) Successful in 2m37s
/ ls (push) Successful in 2m41s

This commit is contained in:
lubiana 2025-01-25 18:07:49 +01:00
parent e3157c1c50
commit 232878ebfb
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
2 changed files with 29 additions and 15 deletions

View file

@ -28,6 +28,17 @@ final class MenuItemController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
foreach ($menuItem->getFoodVendor()->getMenuItems() as $vendorItem) {
if ($menuItem->getAliases()->contains($vendorItem)) {
$vendorItem->setAliasOf($menuItem);
} elseif ($vendorItem->getAliasOf() === $menuItem) {
$vendorItem->setAliasOf(null);
}
$entityManager->persist($vendorItem);
}
$entityManager->persist($menuItem);
$entityManager->flush();
return $this->redirectToRoute('app_menu_item_show', [

View file

@ -11,7 +11,9 @@ use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Uid\Ulid;
use function array_map;
use function assert;
final class MenuItemType extends AbstractType
@ -30,28 +32,29 @@ final class MenuItemType extends AbstractType
'multiple' => true,
'expanded' => true,
'query_builder' => static function (MenuItemRepository $repository) use ($item, $vendorId): QueryBuilder {
$qb = $repository->createQueryBuilder('m');
$ids = $repository->createQueryBuilder('m')
->select('DISTINCT IDENTITY(m.aliasOf)')
->where('m.deletedAt IS NULL')
->andWhere('m.aliasOf IS NOT NULL')
->getquery();
$ids = $ids->getScalarResult();
$ids = array_map(static fn(array $id): Ulid => Ulid::fromBinary($id[1]), $ids);
// Build the main query with a NOT EXISTS constraint
$qb = $repository->createQueryBuilder('m');
$qb
->where('m.foodVendor = :vendorId')
->andWhere('m.deletedAt IS NULL')
->andWhere('m.id != :id')
->andWhere(
$qb->expr()
->notIn(
'm.id',
$repository->createQueryBuilder('m2')
->select('m2.id')
->where('m2.aliasOf != m.id') // Reference m.id in the inner query
->orWhere('m2.aliasOf IS NOT NULL')
->getDQL() // The subquery's DQL string
)
)
->andWhere('m.id != :id');
foreach ($ids as $key => $id) {
$qb->andWhere("m.id != :idBy{$key}");
$qb->setParameter("idBy{$key}", $id, UlidType::NAME);
}
$qb
->orderBy('m.name', 'ASC')
->setParameter('vendorId', $vendorId, UlidType::NAME) // ULID or appropriate type
->setParameter('id', $item->getId(), UlidType::NAME); // ULID or appropriate type
->setParameter('id', $item->getId()); // ULID or appropriate type
return $qb;
},
]);