是否可以在configureListFields的sonataadmin中进行自定义查询? 。
在此功能中:
protected function configureListFields(ListMapper $listMapper) { $listMapper ->>add(.... ; }
谢谢 !
您应该这样重写createQuery
方法( 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,您不能更改查询的SELECT
部分,也不能使用GROUP BY
,因为Sonata在内部至少运行两次此查询。 首先,它检查查询返回多少行。 其次,它在分页运行此查询。
正如Tautrimas所说,您可以在管理类中重写createQuery($context = 'list')
函数。
您可以尝试像这样更改查询的SELECT部分:
$query = parent::createQuery($context);
$query->add('select', 'm', false );
$query->add('from', 'Toto\MyBundle\Entity\MyEntity m', false );
add函数中的第三个参数是一个布尔值,用于选择追加还是替换查询部分。