Search code examples
symfonydoctrine-ormautocompletesonata-admin

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.


Solution

  • Fonctionnality is now bundled with Sonata as simple as:

    $filter->add('label', ModelFilter::class, ['field_type'=>ModelAutocompleteType::class,'field_options' => ['property' => 'name']])