简体   繁体   English

datagrid过滤关系对象作为symfony 2.4中sonata admin的文本字段(下拉列表)

[英]datagrid filter for relation object as text field (insted of dropdown) in sonata admin in symfony 2.4

I have entity 'Action' with relation to 'User'. 我有与'User'有关的实体'Action'。 Created Admin CRUD controller in SonataAdminBundle. 在SonataAdminBundle中创建了Admin CRUD控制器。 Everything works fine except user filter is rendered as dropdown list. 除了用户过滤器呈现为下拉列表外,一切正常。 I have 8k user count and growing so you must see why this is a problem. 我有8k用户数和增长,所以你必须明白为什么这是一个问题。
I want user filter to be text input and on submit to search with LIKE %username% 我希望用户过滤器是文本输入,然后提交以使用LIKE %username%进行搜索

Right now I add user filter like this - $datagridMapper->add('user') . 现在我添加这样的用户过滤器 - $datagridMapper->add('user')

I know I can add filter type and field type but I am not able to find the right combination and options. 我知道我可以添加过滤器类型和字段类型,但我无法找到正确的组合和选项。 Found information on http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html but still no success. http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html上找到了相关信息,但仍然没有成功。

Final solution 最终解决方案

Following Alex Togo answer I used this code: 关注Alex Togo回答我用过这段代码:

$datagridMapper->add('user', 'doctrine_orm_callback', array(
'callback' => function($queryBuilder, $alias, $field, $value) {
    if (empty($value['value'])) {
        return;
    }
    $queryBuilder->leftJoin(sprintf('%s.user', $alias), 'u');
    $queryBuilder->where('u.username LIKE :username');
    $queryBuilder->setParameter('username', '%'.$value['value'].'%');
    return true;
},
'field_type' => 'text'
))

I needed something like this on a project. 我在项目上需要这样的东西。 I implemented this feature using this . 我用这个实现了这个功能。 You can try to set the 'field_type' option to 'text' (I used 'choice' in the project I worked at) and add the querybuilder actions you need to filter. 您可以尝试将'field_type'选项设置为'text'(我在我工作的项目中使用'choice')并添加您需要过滤的querybuilder操作。

Use doctrine_orm_choice option. 使用doctrine_orm_choice选项。

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper->add(
            'module',
            'doctrine_orm_choice',
            [],
            'choice',
            [
                'choices' => $this->filterModuleList
            ]
        )

        ....

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

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