简体   繁体   中英

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

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.

Use doctrine_orm_choice option.

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

        ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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