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

蹲街弑〆低调 提交于 2020-01-12 19:00:13

问题


I have entity 'Action' with relation to 'User'. Created Admin CRUD controller in SonataAdminBundle. 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.
I want user filter to be text input and on submit to search with LIKE %username%

Right now I add user filter like this - $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.

Final solution

Following Alex Togo answer I used this code:

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

回答1:


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.




回答2:


Use doctrine_orm_choice option.

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

        ....


来源:https://stackoverflow.com/questions/22932156/datagrid-filter-for-relation-object-as-text-field-insted-of-dropdown-in-sonata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!