简体   繁体   中英

Autocomplete filter in sonata admin mongodb

I have large collection of cities. I want in my admin to filter by city name. Because of large number of records i have to use sonata_type_model_autocomplete. It works almost perfect in form.

            $form->add('city', 'sonata_type_model_autocomplete',
            [
                'property' => 'name',
                'model_manager' => $form->getAdmin()->getModelManager(),
                'class' => 'AppBundle\Document\Geography\City',
                'to_string_callback' => function($e, $p) {
                    return sprintf('%s, %s, %s, %s',
                        $e->getName(), $e->getRegion()->getName(), $e->getRegion()->getCountry()->getName(), $e->getRegion()->getCountry()->getContinent()->getName()
                    );
                }
            ]);

But when i try to do this in filter i have an error:

Error: Call to a member function isGranted() on null

Code for filter:

            $datagridMapper->add('city', null, ['show_filter' => true], 'sonata_type_model_autocomplete',
            [
                'property' => 'name',
                'context' => 'filter',
                'admin_code' => 'app.admin.custom_page_text',
                'model_manager' => $datagridMapper->getAdmin()->getModelManager(),
                'by_reference' => 'false',
                'to_string_callback' => function($e, $p) {
                    return sprintf('%s, %s, %s, %s',
                        $e->getName(), $e->getRegion()->getName(), $e->getRegion()->getCountry()->getName(), $e->getRegion()->getCountry()->getContinent()->getName()
                    );
                }
            ])

So it will work well with dirty hack. I replaced default view sonata_type_model_autocomplete.html.twig with my own, where i remove line '_context': 'filter' . Hope this solution can be useful for somebody.

It seems to be a bug. I've opened a new pull request to fix this problem.

https://github.com/sonata-project/SonataDoctrineMongoDBAdminBundle/pull/287

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