简体   繁体   English

SonataAdminBundle:创建自定义过滤器类型

[英]SonataAdminBundle: Create custom filter type

Is there a way to create a custom filter type for SonataAdminBundle, that extends another type, for example the type doctrine_orm_callback? 有没有一种方法可以为SonataAdminBundle创建自定义过滤器类型,该类型可以扩展其他类型,例如doctrine_orm_callback类型? The idea is for re-use the custom type in other filters 这个想法是在其他过滤器中重用自定义类型

public function configureDatagridFilters(DatagridMapper $datagridMapper)
{

    $datagridMapper
        ->add('field', 'doctrine_orm_callback', array(
            'callback' => function($queryBuilder, $alias, $field, $value) {
                if (!$value['value']) {
                    return;
                }

                $queryBuilder->leftJoin(sprintf('%s.field', $alias), 'field')
                    ->andWhere("field LIKE :field")
                    ->setParameter('field', "%{$value['value']}%");

                return true;
            },
            'field_type' => 'search'), null, array('pattern' => '^[A-Za-z0-9]{1,12}$'));
}

Thanks! 谢谢!

Check this link and try: 检查此链接,然后尝试:

->add('with_open_comments', 
      'doctrine_orm_callback', 
      array('callback'   => array($this, 'yourFunction'),
            'field_type' => 'search'), 
      null, 
      array('pattern' => '^[A-Za-z0-9]{1,12}$'));

    public function yourFunction($queryBuilder, $alias, $field, $value)
    {
        if (!$value['value']) {
           return;
        }

        $queryBuilder->leftJoin(sprintf('%s.field', $alias), 'field')
                    ->andWhere("field LIKE :field")
                    ->setParameter('field', "%{$value['value']}%");

        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM