Compare commits
No commits in common. "e3157c1c50264a94680140172438e3cd9c7af393" and "0ce09437a38a52f5784f52faf579934b287e79cf" have entirely different histories.
e3157c1c50
...
0ce09437a3
1 changed files with 12 additions and 35 deletions
|
@ -12,49 +12,26 @@ 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;
|
||||||
|
|
||||||
use function assert;
|
|
||||||
|
|
||||||
final class MenuItemType extends AbstractType
|
final class MenuItemType extends AbstractType
|
||||||
{
|
{
|
||||||
#[Override]
|
#[Override]
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||||
{
|
{
|
||||||
$item = $options['data'];
|
$vendor = $options['data']->getFoodVendor();
|
||||||
assert($item instanceof MenuItem); // Ensure it's of the correct type
|
$vendorId = $vendor->getId();
|
||||||
$vendorId = $item->getFoodVendor()?->getId(); // Use safe navigation operator in case FoodVendor is null
|
$builder
|
||||||
|
->add('name')
|
||||||
$builder->add('name'); // Basic field
|
->add('aliasOf', EntityType::class, [
|
||||||
$builder->add('aliases', EntityType::class, [
|
|
||||||
'class' => MenuItem::class,
|
'class' => MenuItem::class,
|
||||||
'choice_label' => 'name',
|
'choice_label' => 'name',
|
||||||
'multiple' => true,
|
'query_builder' => static fn(MenuItemRepository $repository): QueryBuilder => $repository
|
||||||
'expanded' => true,
|
->createQueryBuilder('m')
|
||||||
'query_builder' => static function (MenuItemRepository $repository) use ($item, $vendorId): QueryBuilder {
|
|
||||||
$qb = $repository->createQueryBuilder('m');
|
|
||||||
|
|
||||||
// Build the main query with a NOT EXISTS constraint
|
|
||||||
$qb
|
|
||||||
->where('m.foodVendor = :vendorId')
|
->where('m.foodVendor = :vendorId')
|
||||||
->andWhere('m.deletedAt IS NULL')
|
->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
|
|
||||||
)
|
|
||||||
)
|
|
||||||
->orderBy('m.name', 'ASC')
|
->orderBy('m.name', 'ASC')
|
||||||
->setParameter('vendorId', $vendorId, UlidType::NAME) // ULID or appropriate type
|
->setParameter(':vendorId', $vendorId, UlidType::NAME),
|
||||||
->setParameter('id', $item->getId(), UlidType::NAME); // ULID or appropriate type
|
])
|
||||||
|
;
|
||||||
return $qb;
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Override]
|
#[Override]
|
||||||
|
|
Loading…
Add table
Reference in a new issue