is it possible to make a custom query in sonataadmin in configureListFields ? .
in this function :
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->>add(....
;
}
thank you !
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.
As Tautrimas said, you can override the createQuery($context = 'list')
function in your admin class.
You can try to change the SELECT part of the query like this :
$query = parent::createQuery($context);
$query->add('select', 'm', false );
$query->add('from', 'Toto\MyBundle\Entity\MyEntity m', false );
The third parameter in the add function is a boolean to choose either to append or to replaces the query part.