Hamza Najemi
Hamza Najemi

Reputation: 243

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" ??

Upvotes: 1

Views: 1751

Answers (1)

Fatih Kahveci
Fatih Kahveci

Reputation: 440

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;
}

Upvotes: 3

Related Questions