繁体   English   中英

奏鸣曲管理员configureListFields

[英]Sonata Admin configureListFields

是否可以在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函数中的第三个参数是一个布尔值,用于选择追加还是替换查询部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM