sonata-project / SonataDoctrineORMAdminBundle

Integrate Doctrine ORM into the SonataAdminBundle
https://docs.sonata-project.org/projects/SonataDoctrineORMAdminBundle
MIT License
445 stars 344 forks source link

ModelAutocompleteType don't work with Doctrine CallbackFilter #838

Closed sashaaro closed 3 years ago

sashaaro commented 6 years ago

Environment

Sonata packages

sonata-project/admin-bundle              3.35.2 3.35.2 The missing Symfony Admin Generator
sonata-project/block-bundle              3.12.1 3.12.1 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.9.1  3.9.1  Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.3.1  2.3.1  Symfony SonataDatagridBundle
sonata-project/doctrine-orm-admin-bundle 3.6.0  3.6.1  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  1.9.0  1.9.0  Lightweight Exporter library

Symfony packages

friendsofsymfony/rest-bundle             2.3.1              This Bundle provides various tools to rapidly develop RESTful API's with Symfony
sentry/sentry-symfony                    2.0.2              Symfony integration for Sentry (http://getsentry.com)
symfony/asset                            v4.0.9             Symfony Asset Component
symfony/browser-kit                      v4.0.9             Symfony BrowserKit Component
symfony/cache                            v4.0.9             Symfony Cache component with PSR-6, PSR-16, and tags
symfony/class-loader                     v3.4.9             Symfony ClassLoader Component
symfony/config                           v4.0.9             Symfony Config Component
symfony/console                          v4.0.9             Symfony Console Component
symfony/css-selector                     v4.0.9             Symfony CssSelector Component
symfony/debug                            v4.0.9             Symfony Debug Component
symfony/debug-bundle                     v4.0.11            Symfony DebugBundle
symfony/debug-pack                       v1.0.5             A debug pack for Symfony projects
symfony/dependency-injection             v4.0.9             Symfony DependencyInjection Component
symfony/doctrine-bridge                  v4.0.9             Symfony Doctrine Bridge
symfony/dom-crawler                      v4.0.9             Symfony DomCrawler Component
symfony/dotenv                           v4.0.9             Registers environment variables from a .env file
symfony/event-dispatcher                 v4.0.9             Symfony EventDispatcher Component
symfony/expression-language              v4.0.9             Symfony ExpressionLanguage Component
symfony/filesystem                       v4.0.9             Symfony Filesystem Component
symfony/finder                           v4.0.9             Symfony Finder Component
symfony/flex                             v1.0.80           
symfony/form                             v4.0.9             Symfony Form Component
symfony/framework-bundle                 v4.0.9             Symfony FrameworkBundle
symfony/http-foundation                  v4.0.9             Symfony HttpFoundation Component
symfony/http-kernel                      v4.0.9             Symfony HttpKernel Component
symfony/inflector                        v4.0.9             Symfony Inflector Component
symfony/intl                             v4.0.9             A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/lts                              dev-master 6de50b2 Enforces Long Term Supported versions of Symfony components
symfony/maker-bundle                     v1.4.4             Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about ...
symfony/monolog-bridge                   v4.0.9             Symfony Monolog Bridge
symfony/monolog-bundle                   v3.2.0             Symfony MonologBundle
symfony/options-resolver                 v4.0.9             Symfony OptionsResolver Component
symfony/orm-pack                         v1.0.5             A pack for the Doctrine ORM
symfony/phpunit-bridge                   v4.0.9             Symfony PHPUnit Bridge
symfony/polyfill-intl-icu                v1.8.0             Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring                v1.8.0             Symfony polyfill for the Mbstring extension
symfony/polyfill-php72                   v1.8.0             Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/profiler-pack                    v1.0.3             A pack for the Symfony web profiler
symfony/property-access                  v4.0.9             Symfony PropertyAccess Component
symfony/property-info                    v4.0.9             Symfony Property Info Component
symfony/routing                          v4.0.9             Symfony Routing Component
symfony/security                         v4.0.9             Symfony Security Component
symfony/security-acl                     v3.0.1             Symfony Security Component - ACL (Access Control List)
symfony/security-bundle                  v4.0.9             Symfony SecurityBundle
symfony/stopwatch                        v4.0.9             Symfony Stopwatch Component
symfony/templating                       v4.0.9             Symfony Templating Component
symfony/translation                      v4.0.9             Symfony Translation Component
symfony/twig-bridge                      v4.0.9             Symfony Twig Bridge
symfony/twig-bundle                      v4.0.9             Symfony TwigBundle
symfony/validator                        v4.0.9             Symfony Validator Component
symfony/var-dumper                       v4.0.9             Symfony mechanism for exploring and dumping PHP variables
symfony/web-profiler-bundle              v4.0.9             Symfony WebProfilerBundle
symfony/webpack-encore-pack              v1.0.2             A pack for Symfony Encore
symfony/yaml                             v4.0.9             Symfony Yaml Component

PHP version

PHP 7.1.16 (cli) (built: Apr  5 2018 04:48:10) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

Subject

Sonata\AdminBundle\Form\Type\ModelAutocompleteType not work correct with filter Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter I want Autocomplete filter of releted entity filed. In my exmaple. Teacher's name autocomplete and then search student by reletion. My conclusion CallbackFilter can't work correct with formtype ModelAutocompleteType

Steps to reproduce

$datagridMapper->add('teacher', CallbackFilter::class, [
            'callback' => function (ProxyQuery $proxyQuery, $alias, $field, $value) {
                if (!$value['value']) {
                    return;
                }
                /** @var QueryBuilder $queryBuilder */
                $queryBuilder = $proxyQuery->getQueryBuilder();
                $queryBuilder->leftJoin(sprintf('%s.studyGroup', $alias), 'studyGroup');
                $queryBuilder->leftJoin('studyGroup.teachers', 'teacher');
                $queryBuilder->andWhere('teacher.id = :value');
                $queryBuilder->setParameter(':value', $value['value']);
            },
        ], ModelAutocompleteType::class, [
            'class' => Teacher::class,
            'property' => 'id',
            'context' => 'filter',
            'model_manager' => $this->getModelManager(),//$teacherAdmin->getModelManager(),
            'admin_code' => $this->getCode(),//$teacherAdmin->getCode(),
        ]);

I can't set correct options for right work. I try many variants of settings and it always it throws exception. I start doubt about can it work correct together.

Expected results

CallbackFilter work with ModelAutocompleteType. I can found teachers from Autocomplete and then apply filter for student searching by related field

Actual results

When I start seach in autocomplete I recived for example throw exception No associated entity with field "teacher".

in vendor/sonata-project/admin-bundle/src/Controller/HelperController.php (line 515)
HelperController->retrieveFilterFieldDescription(object(StudentAdmin), 'teacher')
in vendor/sonata-project/admin-bundle/src/Controller/HelperController.php (line 353)
Metabor commented 4 years ago

I run into the same problem, so I can at least approve, that it still exists.

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

VincentLanglet commented 4 years ago

I run into the same problem, so I can at least approve, that it still exists.

Hi @Metabor. Did you tried the last version ? Is it still here ? What is the errors you get ? Do you have a repo to reproduce this ? I'll be happy to take a look if so.

VincentLanglet commented 3 years ago

I just tried and it works fine for me image

With the following config image