2013-09-26 60 views
0

我正在使用sonataAdminBundle和symfony2sonataAdminBundle上的query_builder被忽略

我做了這個腳本。 它過濾搜索框的候選人。

但是這個query_builder被忽略。

還有其他要檢查的地方嗎?

public function configureDatagridFilters(DatagridMapper $datagridMapper) 
{ 

    $datagridMapper 
     ->add('user',null, 
      array(
       'query_builder' => 
       function (\Doctrine\ORM\EntityRepository $rep) { 
        return $rep-> 
        createQueryBuilder('s') 
        ->where('s.id','1'); 
      })  

加入...

我使用這個腳本的新條目,它的工作原理

$formMapper 
     ->with('General') 
      ->add('teacher', 
       null, 
       array(
        'query_builder' => 
        function (\Doctrine\ORM\EntityRepository $rep) { 
        return $rep-> 
        createQueryBuilder('s') 
        ->join('s.groups', 'g') // Assuming the association on your user entity is 'groups' 
        ->where('g.name = :group')->setParameter('group','TeacherGroup'); 
        }) 

      ) 

但datagridMapper,它不工作。

$datagridMapper 
     ->add('teacher',null, 
      array(
       'query_builder' => 
       function (\Doctrine\ORM\EntityRepository $rep) { 
       return $rep-> 
       createQueryBuilder('s') 
       ->join('s.groups', 'g') // Assuming the association on your user entity is 'groups' 
       ->where('g.name = :group') 
       ->setParameter('group','TeacherGroup'); 
      })) 

回答

0

我已經通過以下方式實現了這一

$datagridMapper 
    ->add('user',null, 
     array(
      'field_type' => 'entity', 
      'field_options => array(
       'query_builder' => function (\Doctrine\ORM\EntityRepository $rep) { 
       return $rep-> 
       createQueryBuilder('s') 
       ->where('s.id','1'); 
       } 
      ) 
     });  
0

你應該在第四個參數使用「query_builder」,是這樣的:

$datagridMapper 
    ->add('teacher',null,null, 
     array(
      'query_builder' => 
      function (\Doctrine\ORM\EntityRepository $rep) { 
      return $rep-> 
      createQueryBuilder('s') 
      ->join('s.groups', 'g') // Assuming the association on your user entity is 'groups' 
      ->where('g.name = :group') 
      ->setParameter('group','TeacherGroup'); 
     }))