sort and filter menuitems in aliasof select
Some checks failed
/ ls (pull_request) Failing after 2m56s
Some checks failed
/ ls (pull_request) Failing after 2m56s
This commit is contained in:
parent
9781bd561f
commit
889ed63bc5
1 changed files with 32 additions and 13 deletions
|
@ -14,24 +14,43 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
final class MenuItemType extends AbstractType
|
final class MenuItemType extends AbstractType
|
||||||
{
|
{
|
||||||
#[Override]
|
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||||
{
|
{
|
||||||
$vendor = $options['data']->getFoodVendor();
|
$item = $options['data'];
|
||||||
$vendorId = $vendor->getId();
|
assert($item instanceof MenuItem); // Ensure it's of the correct type
|
||||||
$builder
|
$vendorId = $item->getFoodVendor()?->getId(); // Use safe navigation operator in case FoodVendor is null
|
||||||
->add('name')
|
|
||||||
->add('aliasOf', EntityType::class, [
|
$builder->add('name'); // Basic field
|
||||||
|
$builder->add('aliases', EntityType::class, [
|
||||||
'class' => MenuItem::class,
|
'class' => MenuItem::class,
|
||||||
'choice_label' => 'name',
|
'choice_label' => 'name',
|
||||||
'query_builder' => static fn(MenuItemRepository $repository): QueryBuilder => $repository
|
'multiple' => true,
|
||||||
->createQueryBuilder('m')
|
'expanded' => true,
|
||||||
->where('m.foodVendor = :vendorId')
|
'query_builder' => static function (MenuItemRepository $repository) use ($item, $vendorId): QueryBuilder {
|
||||||
->setParameter(':vendorId', $vendorId, UlidType::NAME),
|
$qb = $repository->createQueryBuilder('m');
|
||||||
])
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Build the main query with a NOT EXISTS constraint
|
||||||
|
$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
|
||||||
|
)
|
||||||
|
)
|
||||||
|
->orderBy('m.name', 'ASC')
|
||||||
|
->setParameter('vendorId', $vendorId, UlidType::NAME) // ULID or appropriate type
|
||||||
|
->setParameter('id', $item->getId(), UlidType::NAME); // ULID or appropriate type
|
||||||
|
|
||||||
|
return $qb;
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
#[Override]
|
#[Override]
|
||||||
public function configureOptions(OptionsResolver $resolver): void
|
public function configureOptions(OptionsResolver $resolver): void
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue