sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Inherited classes and ModelAutocompleteType: The field "xxx" does not exist. #5468

Closed ger86 closed 3 years ago

ger86 commented 5 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.45.1 3.45.1 The missing Symfony Admin Generator
sonata-project/block-bundle              3.14.0 3.14.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.15.1 3.15.1 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.4.0  2.4.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.1.5  1.1.5  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.8.1  3.8.2  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  1.11.0 1.11.0 Lightweight Exporter library

Symfony packages

$ composer show --latest 'symfony/*'
symfony/apache-pack           v1.0.1  v1.0.1  A pack for Apache support in Symfony
symfony/asset                 v4.2.2  v4.2.3  Symfony Asset Component
symfony/browser-kit           v4.2.3  v4.2.3  Symfony BrowserKit Component
symfony/cache                 v4.2.2  v4.2.3  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/config                v4.2.2  v4.2.3  Symfony Config Component
symfony/console               v4.2.2  v4.2.3  Symfony Console Component
symfony/contracts             v1.0.2  v1.0.2  A set of abstractions extracted out of the Symfony components
symfony/css-selector          v4.2.3  v4.2.3  Symfony CssSelector Component
symfony/debug                 v4.2.2  v4.2.3  Symfony Debug Component
symfony/debug-bundle          v4.2.2  v4.2.3  Symfony DebugBundle
symfony/debug-pack            v1.0.7  v1.0.7  A debug pack for Symfony projects
symfony/dependency-injection  v4.2.2  v4.2.3  Symfony DependencyInjection Component
symfony/doctrine-bridge       v4.2.2  v4.2.3  Symfony Doctrine Bridge
symfony/dom-crawler           v4.2.2  v4.2.3  Symfony DomCrawler Component
symfony/dotenv                v4.2.2  v4.2.3  Registers environment variables from a .env file
symfony/event-dispatcher      v4.2.2  v4.2.3  Symfony EventDispatcher Component
symfony/expression-language   v4.2.2  v4.2.3  Symfony ExpressionLanguage Component
symfony/filesystem            v4.2.2  v4.2.3  Symfony Filesystem Component
symfony/finder                v4.2.2  v4.2.3  Symfony Finder Component
symfony/flex                  v1.1.8  v1.1.8  Composer plugin for Symfony
symfony/form                  v4.2.2  v4.2.3  Symfony Form Component
symfony/framework-bundle      v4.2.2  v4.2.3  Symfony FrameworkBundle
symfony/http-foundation       v4.2.2  v4.2.3  Symfony HttpFoundation Component
symfony/http-kernel           v4.2.2  v4.2.3  Symfony HttpKernel Component
symfony/inflector             v4.2.2  v4.2.3  Symfony Inflector Component
symfony/intl                  v4.2.2  v4.2.3  A PHP replacement layer for the C intl extension that includes additional data from the ICU li...
symfony/maker-bundle          v1.11.3 v1.11.3 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so yo...
symfony/monolog-bridge        v4.2.2  v4.2.3  Symfony Monolog Bridge
symfony/monolog-bundle        v3.3.1  v3.3.1  Symfony MonologBundle
symfony/options-resolver      v4.2.2  v4.2.3  Symfony OptionsResolver Component
symfony/orm-pack              v1.0.6  v1.0.6  A pack for the Doctrine ORM
symfony/panther               v0.2.0  v0.2.0  A browser testing and web scraping library for PHP and Symfony.
symfony/phpunit-bridge        v4.2.3  v4.2.3  Symfony PHPUnit Bridge
symfony/polyfill-intl-icu     v1.10.0 v1.10.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring     v1.10.0 v1.10.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72        v1.10.0 v1.10.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/process               v4.2.2  v4.2.3  Symfony Process Component
symfony/profiler-pack         v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access       v4.2.2  v4.2.3  Symfony PropertyAccess Component
symfony/property-info         v4.2.2  v4.2.3  Symfony Property Info Component
symfony/routing               v4.2.2  v4.2.3  Symfony Routing Component
symfony/security-acl          v3.0.1  v3.0.1  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle       v4.2.2  v4.2.3  Symfony SecurityBundle
symfony/security-core         v4.2.2  v4.2.3  Symfony Security Component - Core Library
symfony/security-csrf         v4.2.2  v4.2.3  Symfony Security Component - CSRF Library
symfony/security-guard        v4.2.2  v4.2.3  Symfony Security Component - Guard
symfony/security-http         v4.2.2  v4.2.3  Symfony Security Component - HTTP Integration
symfony/serializer            v4.2.2  v4.2.3  Symfony Serializer Component
symfony/serializer-pack       v1.0.2  v1.0.2  A pack for the Symfony serializer
symfony/stopwatch             v4.2.2  v4.2.3  Symfony Stopwatch Component
symfony/swiftmailer-bundle    v3.2.5  v3.2.5  Symfony SwiftmailerBundle
symfony/templating            v4.2.2  v4.2.3  Symfony Templating Component
symfony/test-pack             v1.0.5  v1.0.5  A pack for functional and end-to-end testing within a Symfony app
symfony/translation           v4.2.2  v4.2.3  Symfony Translation Component
symfony/twig-bridge           v4.2.2  v4.2.3  Symfony Twig Bridge
symfony/twig-bundle           v4.2.2  v4.2.3  Symfony TwigBundle
symfony/validator             v4.2.2  v4.2.3  Symfony Validator Component
symfony/var-dumper            v4.2.2  v4.2.3  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter          v4.2.2  v4.2.3  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link              v4.2.2  v4.2.3  Symfony WebLink Component
symfony/web-profiler-bundle   v4.2.2  v4.2.3  Symfony WebProfilerBundle
symfony/web-server-bundle     v4.2.2  v4.2.3  Symfony WebServerBundle
symfony/webpack-encore-bundle v1.1.0  v1.1.0  Integration with your Symfony app & Webpack Encore!
symfony/yaml                  v4.2.2  v4.2.3  Symfony Yaml Component

PHP version

$ php -v
PHP 7.1.20 (cli) (built: Aug 28 2018 16:30:44) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

Subject

I have a class HistoryRecord from which a class CollaboHistoryRecord inherits. I have declared and admin for class A in the following way:

    admin.history_record:
        class: App\Admin\HistoryRecordAdmin
        arguments: [~, App\Entity\History\HistoryRecord, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, group: admin, label: HistoryRecord }
        public: true
        calls:
            - method: setSubClasses
              arguments:
                - historyRecord: App\Entity\History\CollaborationHistoryRecord

In the admin I have the following in the configureFormFields method:

       ....
        $subject = $this->getSubject();
        if ($subject instanceof CollaborationHistoryRecord) {
            $formMapper->add('collaboration', ModelAutocompleteType::class, [
                'property' => ['person'],
                'class' => 'App\Entity\Collaboration'
            ]);
        }

The problem happens when I try to use the autocomplete field: a 500 error happens with the following message:

The field "collaboration" does not exist.

Steps to reproduce

  1. Create an entity A and an entity B which inherits from A. Add to entity B a ManyToOne field related to other entity (called C).
  2. Create an admin for A using the hability of SonataAdmin to handle inhertied classes.
  3. Declare an autocomplete field for the relationship between B and C
  4. Try to search a C object

Expected results

  1. Get the C object attached to the field

Actual results

RuntimeException:
The field "collaboration" does not exist.

  at vendor/sonata-project/admin-bundle/src/Action/RetrieveAutocompleteItemsAction.php:214
  at Sonata\AdminBundle\Action\RetrieveAutocompleteItemsAction->retrieveFormFieldDescription(object(HistoryRecordAdmin), 'collaboration')
     (vendor/sonata-project/admin-bundle/src/Action/RetrieveAutocompleteItemsAction.php:72)
  at Sonata\AdminBundle\Action\RetrieveAutocompleteItemsAction->__invoke(object(Request))
     (vendor/symfony/http-kernel/HttpKernel.php:150)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:67)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:198)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:25)
stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

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

simplegr commented 4 years ago

I had a similar issue with conditions in configureFormFields. When autocomplete runs $this->getSubject() is null. This is how I solved it:

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $editSubject = $this->id($this->getSubject());

        //...
        // Show only when edit or autocomplete items action
        if ($editSubject || 'sonata_admin_retrieve_autocomplete_items' === $this->getRequest()->get('_route')) {
            $formMapper
                ->add('myField', ModelAutocompleteType::class, [
                     //...
                    'callback' => [$this, 'myCallback'],
                ]);
            //...
        }
      }
      //...
}
tonyellow commented 4 years ago

I am experiencing exactly the same as the topic starter in combination with inherited class.

github-actions[bot] commented 3 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.