Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'. 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'
))
like image 466
Aurelijus Rozenas Avatar asked Apr 08 '14 08:04

Aurelijus Rozenas


1 Answers

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.

like image 151
Alexandru Togorean Avatar answered Sep 17 '22 19:09

Alexandru Togorean