I use LexikFormFilterBundle to filter orders but I'dont know why condition does not work works with the bundle: oa.status = active when I filter by "status"
Why the bundle loses conditions from getAllForActiveAccount function?
controller:
$orders = $orderRepository->getAllForActiveAccount();
$form = $this->createForm(OrderFilterType::class);
if ($request->query->has($form->getName())) {
$form->submit($request->query->get($form->getName()));
$filterBuilderUpdater->addFilterConditions($form, $orders);
}
Order filter type:
class OrderFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('account', Filters\EntityFilterType::class, [
'label' => 'Account',
'class' => Account::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('oa')
->where('oa.status = \'active\'');
},
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'row_attr' => [
'class' => 'col-12 col-sm-4',
]
]);
$builder->add('status', Filters\ChoiceFilterType::class, [
'label' => 'Status',
'multiple' => true,
'expanded' => true,
'choices' => \array_flip(Order::statusNames),
'choice_label' => function ($choice, $key, $value) {
return $key;
},
'row_attr' => [
'class' => 'col-12 col-sm-4',
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_protection' => false,
'validation_groups' => ['filtering'],
'method' => Request::METHOD_GET,
'attr' => [
'class' => 'row',
]
]);
}
public function getBlockPrefix()
{
return 'order_filter';
}
}
OrderRepository:
public function getAllForActiveAccount(): QueryBuilder
{
return $this->createQueryBuilder('o')
->join("o.account", "oa")
->where('oa.status = \'active\'');
}
My Answer:
status = active.
To troubleshoot this issue, you can try the following steps:
1. Check your filter form configuration: Make sure that the filter form is correctly configured to filter orders based on the status field. Double-check the field name and type in the filter form class.
2. Check your entity configuration: Ensure that the status field is correctly mapped in your entity class and that it is properly linked to the orders entity.
3. Check your filter query: Verify that the filter query is correctly set up to filter orders based on the status field. Check if the condition oa.status = active is correctly applied in the filter query.
4. Debug the filter query: You can debug the filter query to see if the condition oa.status = active is being applied correctly. You can use the Symfony profiler or dump the query to see the generated SQL query.
5. Check for any overrides or customizations: Make sure that there are no customizations or overrides in your code that might be affecting the filter conditions.
By following these steps, you should be able to identify the issue and fix the condition oa.status = active not working with LexikFormFilterBundle.
Rate this post
5 of 5 based on 8043 votesComments