Search code examples
symfonysonata-admin

Sonata Admin configureListFields


is it possible to make a custom query in sonataadmin in configureListFields ? .

in this function :

protected function configureListFields(ListMapper $listMapper) { $listMapper ->>add(.... ; }

thank you !


Solution

  • You should override createQuery method like this (source):

    public function createQuery($context = 'list') 
    { 
        $query = parent::createQuery($context); 
        // this is the queryproxy, you can call anything you could call on the doctrine orm QueryBuilder 
        $query->andWhere( 
            $query->expr()->eq($query->getRootAlias().'.username', ':username') 
        ); 
        $query->setParameter('username', 'test'); // eg get from security context 
        return $query; 
    } 
    

    AFAIK, you cannot change SELECT part of the query and you cannot use GROUP BY, because internally Sonata runs this query at least two times. First, it checks how many rows query returns. Second, it runs this query paginated.