Sonata Admin configureListFields

我只是一个虾纸丫 提交于 2019-12-11 11:15:38

问题


is it possible to make a custom query in sonataadmin in configureListFields ? .

in this function :

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

thank you !


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/19211802/sonata-admin-configurelistfields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!