EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.07k stars 1.02k forks source link

Filter on property of an entity in a many-to-one relation #4966

Open ToshY opened 2 years ago

ToshY commented 2 years ago

Describe the bug After not being able to find in the docs on how to apply a filter based on a property of the entity I'd like to ask if it's even possible.

To Reproduce As an example, I have have a many-to-one relation from User to Colour denoted with the property userColour. The Colour entity consists of a property colourCode which is of type string.

In the CRUD controller for User, let's say I have the following fields configured:

return [
    AssociationField::new('userColour'),
    TextField::new('userColour.colourCode'),
];

Now I can use the EntityFilter on userColour to filter (which works), but if I wanted to filter on userColour.colourCode it doesn't seem to be that easy.

return $filters
    ->add(EntityFilter::new('userColour')) // works
    ->add(EntityFilter::new('userColour.colourCode')) // doesn't work

Throwing the following error/stacktrace:

Error:
Call to a member function getAsDto() on array

  at vendor/easycorp/easyadmin-bundle/src/Factory/FilterFactory.php:72
  at EasyCorp\Bundle\EasyAdminBundle\Factory\FilterFactory->create(object(FilterConfigDto), object(FieldCollection), object(EntityDto))
     (vendor/easycorp/easyadmin-bundle/src/Controller/AbstractCrudController.php:124)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController->index(object(AdminContext))
     (vendor/symfony/http-kernel/HttpKernel.php:152)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:74)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (vendor/autoload_runtime.php:29)
  at require_once('/app/vendor/autoload_runtime.php')
     (public/index.php:5) 

So am I missing something obvious here, either wrongly passing the property name or missing some options? Or is this either something that isn't working correctly, or maybe should a custom filter be used for such cases?

Additional context

Versions:

Ang3 commented 2 years ago

Issues: #3171 (2.x) #4245 (3.x) #4881 (3.x/4.x) Pull requests: #4236 #4840 #4882

The dot is a known problem for filters for a while... :(

Still 3 options to filter embedded or association fields (with dot):

ToshY commented 2 years ago

@Ang3 Thanks for the reply. PR #4882 is still open, is there a reason not to merge that one?