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

StackOverflow https://stackoverflow.com/questions/22932156

Question

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'
))
Was it helpful?

Solution

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.

OTHER TIPS

Use doctrine_orm_choice option.

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

        ....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top