Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonata how make autocomplete filter on current entity property

I'm working on a sf2 project using Sonata Admin Bundle. The project is a Donations website for humanitarian mission. I have a 'Personne' entity, represent benefactors (donation makers).

My problem is the following: I have to filter results in the sonata list view using autocompletion. I want filter results using the 'name' property of the current entity ('Personne').

What I'm expecting :

$datagridMapper
    ->add('personne', 'doctrine_orm_model_autocomplete', 
          array('label' => 'AutoComplete'), 
          null, 
          array('property' => 'name'))

// error output : " The option `association_mapping` must be set for field: `personne` "

You can see my full admin class and entity on this gist : https://gist.github.com/chalasr/0658a02b1c04180f5563

I understand this field type is reserved to entity associations (by example I already use it for filter results of my Donation entity by Personne name (other admin class).

My question is : Is it possible to do what I need ? If I can't do that using this field type, what is the right way to achieve this task ?

Thank's for your help.

like image 756
chalasr Avatar asked Dec 06 '25 17:12

chalasr


1 Answers

After a lot of tests, it seems this functionality isn't yet provided by Sonata.

So, I had build an homemade autocomplete method in my admin controller and used it as ajax in my overridden CRUD:list.html.twig template. This method takes field name, other autocomplete fields values, and keyword as parameters and reload results on keyup event.

You can look on this gist: https://gist.github.com/chalasr/5c27ae64dc596967f18a

If you have an idea/proposition for optimize my code (simple autocomplete field type for $formMapper ?), I'm really interested.

like image 73
chalasr Avatar answered Dec 09 '25 12:12

chalasr