Search code examples
phpsymfonysonata-adminsymfony-sonatasonata

How to add filter to configureListField in Sonata Admin Bundle (createQuery method)


this is my ConfigureListFild :

enter image description here

I want to show my account data in ConfigureListFields with condition example (where type='client')

protected function configureListFields(ListMapper $listMapper)
{
    // ... configure $listMapper
    $listMapper
        ->addIdentifier('raison_sociale')
        ->add('type')
        ->add('category.name')
        ->add('portable')
        ->add('ville_fac')
        ->add('professionnel')
        ->add('_action', 'actions', array(
        'actions' => array(
            'show' => array(),
            'edit' => array(),
            'delete' => array(),
        )
    ))
    ;
}

can you help my to show List of account by type = "Client" ??


Solution

  • You need to override createQuery method in your admin class something like that ;

    public function createQuery($context = 'list')
    {
            $query = parent::createQuery($context);
            $rootAlias = $query->getRootAliases()[0];
    
            $query->andWhere(
                $query->expr()->eq($rootAlias.'.type', ':type')
            );
    
            $query->setParameter(':type', 'Client');
    
            return $query;
    }