SonataAdminBundle model_type值以过滤器形式过滤

时间:2012-08-25 02:01:07

标签: symfony symfony-sonata sonata-admin

我有这样的领域

    /**
     * @ORM\ManyToOne(targetEntity="Town")
    **/
    protected $town;

和Admin类使用这种方法

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
        ...
        ->add('town', null, array('label' => 'Town'), null, array('expanded' => true, 'multiple' => true))
        ;
    }

它给了我这样的过滤器:

symfony2 SonataAdminBundle fulter example with doctrine entity 我的问题是:我可以为城镇实体检索设置自定义sql \ dql吗?例如,仅选择ID为IN(1,2)的城镇?

1 个答案:

答案 0 :(得分:8)

好的,我明白了。这是一个解决方案:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
    ...
    ->add('town', null, array('label' => 'Town'), null, array(
        'expanded' => true,
        'multiple' => true,
        'query_builder' => function (\Doctrine\ORM\EntityRepository $repository) {
            return $repository->createQueryBuilder('t')
                    ->where('t.id = ?1')
                    ->setParameter(1, 1)
                    ->add('orderBy', 't.name ASC');
        }
    ))
    ...
    ;
}