奏鸣曲管理员configureListFields

问题描述 投票:1回答:2

是否可以在configureListFields的sonataadmin中进行自定义查询? 。

在此功能中:

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

谢谢 !

symfony sonata-admin
2个回答
1
投票

您应该这样重写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在内部至少运行两次此查询。 首先,它检查查询返回多少行。 其次,它在分页运行此查询。


0
投票

正如Tautrimas所说,您可以在管理类中重写createQuery($context = 'list')函数。

您可以尝试像这样更改查询的SELECT部分​​:

$query = parent::createQuery($context);
$query->add('select', 'm', false );
$query->add('from', 'Toto\MyBundle\Entity\MyEntity m', false );

add函数中的第三个参数是一个布尔值,用于选择追加还是替换查询部分。

© www.soinside.com 2019 - 2024. All rights reserved.