1

I am using sonataAdminBundle and symfony2

I made this script. it filter the search box candidates.

but this query_builder is ignored.

Are there any other points to check ?

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');
             })       

adding...

I am using this script for new entry,it works

    $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');
                    })

            ) 

but for datagridMapper ,it doesnt work.

    $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');
             }))
4

3 回答 3

1

您应该在第四个参数中使用“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');
         }))
于 2015-03-11T11:41:55.517 回答
1

我通过以下方式实现了这一点

 $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');
              }
            )
         });       
于 2014-08-18T06:31:59.893 回答
0

查看有关此的文档。你有错误的语法。 http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html#callback

于 2013-09-26T14:58:54.093 回答