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

Cannot use Autocomplete property as array on Master branche #6779

Closed willemverspyck closed 3 years ago

willemverspyck commented 3 years ago

Environment

Sonata packages

show

``` sonata-project/admin-bundle dev-master 1d3c8b1 The missing Symfony Admin Generator sonata-project/block-bundle 4.4.0 Symfony SonataBlockBundle sonata-project/cache 2.0.1 Cache library sonata-project/doctrine-extensions 1.10.1 Doctrine2 behavioral extensions sonata-project/doctrine-orm-admin-bundle dev-master e354289 Integrate Doctrine ORM into the SonataAdminBundle sonata-project/exporter 2.4.1 Lightweight Exporter library sonata-project/form-extensions 1.7.1 Symfony form extensions sonata-project/twig-extensions 1.4.1 Sonata twig extensions ```

Symfony packages

show

``` symfony/amqp-messenger v5.2.0 Symfony AMQP extension Messenger Bridge symfony/apache-pack v1.0.1 A pack for Apache support in Symfony symfony/asset v5.2.0 Symfony Asset Component symfony/browser-kit v5.2.0 Symfony BrowserKit Component symfony/cache v5.2.0 Symfony Cache component with PSR-6, PSR-16, and tags symfony/cache-contracts v2.2.0 Generic abstractions related to caching symfony/config v5.2.0 Symfony Config Component symfony/console v5.2.0 Symfony Console Component symfony/css-selector v5.2.0 Symfony CssSelector Component symfony/debug-bundle v5.2.0 Symfony DebugBundle symfony/debug-pack v1.0.9 A debug pack for Symfony projects symfony/dependency-injection v5.2.0 Symfony DependencyInjection Component symfony/deprecation-contracts v2.2.0 A generic function and convention to trigger deprecation notices symfony/doctrine-bridge v5.2.0 Symfony Doctrine Bridge symfony/doctrine-messenger v5.2.0 Symfony Doctrine Messenger Bridge symfony/dom-crawler v5.2.0 Symfony DomCrawler Component symfony/dotenv v5.2.0 Registers environment variables from a .env file symfony/error-handler v5.2.0 Symfony ErrorHandler Component symfony/event-dispatcher v5.2.0 Symfony EventDispatcher Component symfony/event-dispatcher-contracts v2.2.0 Generic abstractions related to dispatching event symfony/expression-language v5.2.0 Symfony ExpressionLanguage Component symfony/filesystem v5.2.0 Symfony Filesystem Component symfony/finder v5.2.0 Symfony Finder Component symfony/flex v1.10.0 Composer plugin for Symfony symfony/form v5.2.0 Symfony Form Component symfony/framework-bundle v5.2.0 Symfony FrameworkBundle symfony/http-client v5.2.0 Symfony HttpClient component symfony/http-client-contracts v2.3.1 Generic abstractions related to HTTP clients symfony/http-foundation v5.2.0 Symfony HttpFoundation Component symfony/http-kernel v5.2.0 Symfony HttpKernel Component symfony/intl v5.2.0 A PHP replacement layer for the C intl extension that includes additional data from the ICU library. symfony/mailer v5.2.0 Symfony Mailer Component symfony/maker-bundle v1.24.2 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code. symfony/messenger v5.2.0 Symfony Messenger Component symfony/mime v5.2.0 A library to manipulate MIME messages symfony/monolog-bridge v5.2.0 Symfony Monolog Bridge symfony/monolog-bundle v3.6.0 Symfony MonologBundle symfony/notifier v5.2.0 A library to notify messages symfony/options-resolver v5.2.0 Symfony OptionsResolver Component symfony/orm-pack v2.0.0 A pack for the Doctrine ORM symfony/phpunit-bridge v5.2.0 Symfony PHPUnit Bridge symfony/polyfill-intl-grapheme v1.20.0 Symfony polyfill for intl's grapheme_* functions symfony/polyfill-intl-icu v1.20.0 Symfony polyfill for intl's ICU-related data and classes symfony/polyfill-intl-idn v1.20.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions symfony/polyfill-intl-normalizer v1.20.0 Symfony polyfill for intl's Normalizer class and related functions symfony/polyfill-mbstring v1.20.0 Symfony polyfill for the Mbstring extension symfony/polyfill-php73 v1.20.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions symfony/polyfill-php80 v1.20.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions symfony/process v5.2.0 Symfony Process Component symfony/profiler-pack v1.0.5 A pack for the Symfony web profiler symfony/property-access v5.2.0 Symfony PropertyAccess Component symfony/property-info v5.2.0 Symfony Property Info Component symfony/redis-messenger v5.2.0 Symfony Redis extension Messenger Bridge symfony/routing v5.2.0 Symfony Routing Component symfony/security-acl v3.1.0 Symfony Security Component - ACL (Access Control List) symfony/security-bundle v5.2.0 Symfony SecurityBundle symfony/security-core v5.2.0 Symfony Security Component - Core Library symfony/security-csrf v5.2.0 Symfony Security Component - CSRF Library symfony/security-guard v5.2.0 Symfony Security Component - Guard symfony/security-http v5.2.0 Symfony Security Component - HTTP Integration symfony/serializer v5.2.1 Symfony Serializer Component symfony/serializer-pack v1.0.4 A pack for the Symfony serializer symfony/service-contracts v2.2.0 Generic abstractions related to writing services symfony/stopwatch v5.2.0 Symfony Stopwatch Component symfony/string v5.2.0 Symfony String component symfony/templating v5.2.0 Symfony Templating Component symfony/test-pack v1.0.7 A pack for functional and end-to-end testing within a Symfony app symfony/translation v5.2.0 Symfony Translation Component symfony/translation-contracts v2.3.0 Generic abstractions related to translation symfony/twig-bridge v5.2.0 Symfony Twig Bridge symfony/twig-bundle v5.2.0 Symfony TwigBundle symfony/twig-pack v1.0.1 A Twig pack for Symfony projects symfony/validator v5.2.0 Symfony Validator Component symfony/var-dumper v5.2.0 Symfony mechanism for exploring and dumping PHP variables symfony/var-exporter v5.2.0 A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code symfony/web-link v5.2.0 Symfony WebLink Component symfony/web-profiler-bundle v5.2.0 Symfony WebProfilerBundle symfony/yaml v5.2.0 Symfony Yaml Component ```

PHP version

PHP 7.4.14 (cli)

Subject

I'm already testing the master branche of Sonata with Symfony 5. Maybe the problem is already known, but when using this code:

$formMapper
    ->add('user', ModelAutocompleteType::class, [
    'property' => [
        'firstname',
        'middlename',
        'lastname',
        'email',
    ]
    ]);

With the "property" field as array, so that you can use multiple fields to search in, I get a Uncaught PHP Exception TypeError:

Uncaught PHP Exception TypeError: "Argument 3 passed to Sonata\AdminBundle\Form\DataTransformer\ModelToIdPropertyTransformer::__construct() must be of the type string, array given, called in /vendor/sonata-project/admin-bundle/src/Form/Type/ModelAutocompleteType.php on line 39

Because the parameter in the constructor expects "property" to be a string:

    public function __construct(
        ModelManagerInterface $modelManager,
        string $className,
        string $property,
        bool $multiple = false,
        ?callable $toStringCallback = null
    ) {}
VincentLanglet commented 3 years ago

This is not really a bug since it was already a string on 3.x. https://github.com/sonata-project/SonataAdminBundle/blob/bb7b9ae624336902faed4a12592209d11a599a12/src/Form/DataTransformer/ModelToIdPropertyTransformer.php#L49

Did it work on 3.x for you with an array ? I'm not sure to understand how is used the property param in the code.

If yes, I recommend a PR on 3.x to

VincentLanglet commented 3 years ago

If you dont have a 3.x project, you can check it by removing the string param typehint in your vendor @willemverspyck and see if everything works as expected.

dmaicher commented 3 years ago

Indeed I'm also using this on my project (with 3.x) like

        $form
            ->add('account', ModelAutocompleteType::class, [
                'property' => ['id', 'name'],
            ])

And in the docs listed on Symfony.com it actually also mentions this:

property defaults to null. You have to set this to designate which field (or a list of fields) to use for the choice values. This value can be string or array of strings.

VincentLanglet commented 3 years ago

Can you open a PR on 3.x @dmaicher ?

The construct should be

/**
     * @param string               $className
     * @param string|string[] $property
     * @param bool                 $multiple
     * @param callable|null    $toStringCallback
     *
     * @phpstan-template P of string|string[]
     * @phpstan-param class-string<T> $className
     * @phpstan-param P
     * @phpstan-param null|callable(object, P): string $toStringCallback
     */
    public function __construct(
        ModelManagerInterface $modelManager,
        $className,
        $property,
        $multiple = false,
        $toStringCallback = null
    ) {

then, I think. And we should add a test.

But I didn't find where the property was used...

willemverspyck commented 3 years ago

Wow! You guys are fast 👍

It is used in src/Action/RetrieveAutocompleteItemsAction.php on line 112 (were it's also tested as array).

Thanks for all the work!

VincentLanglet commented 3 years ago

Thanks for all the work!

Thanks to you for beta testing the master branch. We're trying hard to finish the 4.0 release. Be free to report any bug you find.

dmaicher commented 3 years ago

@willemverspyck this should work again on master. Can you confirm?

willemverspyck commented 3 years ago

Yes, it works. Thanks @dmaicher!