2013-10-06 55 views
1

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

在这个函数:

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

谢谢!

回答

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,因为内部索纳塔运行此查询至少两次。首先,它检查查询返回的行数。其次,它运行这个查询分页。

+0

种类的相关[问题](http://stackoverflow.com/questions/24450039/how-to-customize-configuredatagridfilters-in-sonata-admin-to-use-non-related-mon)? – abbood

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函数的第三个参数是一个布尔值,要么选择追加或替换查询部分。