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

Object of class Closure could not be converted to string #6649

Closed Nayden-TaylorAndHart closed 3 years ago

Nayden-TaylorAndHart commented 3 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.x-dev 4993aeb dev-master a8da32a
sonata-project/block-bundle              3.18.5          4.4.0             
sonata-project/cache                     2.0.1           2.0.1             
sonata-project/core-bundle               3.18.0          3.20.0            
Package sonata-project/core-bundle is abandoned, you should avoid using it. No replacement was suggested.
sonata-project/datagrid-bundle           2.5.0           3.2.0             
sonata-project/doctrine-extensions       1.6.0           1.10.1            
sonata-project/doctrine-orm-admin-bundle 3.17.1          3.26.0            
sonata-project/exporter                  2.2.0           2.4.1             
sonata-project/translation-bundle        2.5.0           2.7.0         

Symfony packages

$ composer show --latest 'symfony/*'
Restricting packages listed in "symfony/symfony" to "4.*"
symfony/asset                      v4.4.8  v4.4.17 Symfony Asset Component
symfony/cache                      v4.4.8  v4.4.17 Symfony Cache component w...
symfony/cache-contracts            v2.0.1  v2.2.0  Generic abstractions rela...
symfony/config                     v4.4.8  v4.4.17 Symfony Config Component
symfony/console                    v4.4.8  v4.4.17 Symfony Console Component
symfony/debug                      v4.4.8  v4.4.17 Symfony Debug Component
symfony/debug-bundle               v4.4.8  v4.4.17 Symfony DebugBundle
symfony/debug-pack                 v1.0.8  v1.0.9  A debug pack for Symfony ...
symfony/dependency-injection       v4.4.8  v4.4.17 Symfony DependencyInjecti...
symfony/doctrine-bridge            v4.4.8  v4.4.17 Symfony Doctrine Bridge
symfony/dotenv                     v4.4.8  v4.4.17 Registers environment var...
symfony/error-handler              v4.4.8  v4.4.17 Symfony ErrorHandler Comp...
symfony/event-dispatcher           v4.4.8  v4.4.17 Symfony EventDispatcher C...
symfony/event-dispatcher-contracts v1.1.7  v2.2.0  Generic abstractions rela...
symfony/expression-language        v4.4.8  v4.4.17 Symfony ExpressionLanguag...
symfony/filesystem                 v4.4.8  v4.4.17 Symfony Filesystem Component
symfony/finder                     v4.4.8  v4.4.17 Symfony Finder Component
symfony/flex                       v1.6.3  v1.11.0 Composer plugin for Symfony
symfony/form                       v4.4.8  v4.4.17 Symfony Form Component
symfony/framework-bundle           v4.4.8  v4.4.17 Symfony FrameworkBundle
symfony/http-client                v4.4.8  v4.4.17 Symfony HttpClient component
symfony/http-client-contracts      v2.0.1  v2.3.1  Generic abstractions rela...
symfony/http-foundation            v4.4.8  v4.4.17 Symfony HttpFoundation Co...
symfony/http-kernel                v4.4.8  v4.4.17 Symfony HttpKernel Component
symfony/inflector                  v4.4.8  v4.4.17 Symfony Inflector Component
symfony/intl                       v4.4.8  v4.4.17 A PHP replacement layer f...
symfony/maker-bundle               v1.17.0 v1.24.2 Symfony Maker helps you c...
symfony/mime                       v4.4.8  v4.4.17 A library to manipulate M...
symfony/monolog-bridge             v4.4.8  v4.4.17 Symfony Monolog Bridge
symfony/monolog-bundle             v3.5.0  v3.6.0  Symfony MonologBundle
symfony/options-resolver           v4.4.8  v4.4.17 Symfony OptionsResolver C...
symfony/orm-pack                   v2.0.0  v2.0.0  A pack for the Doctrine ORM
symfony/phpunit-bridge             v5.0.8  v5.2.0  Symfony PHPUnit Bridge
symfony/polyfill-intl-grapheme     v1.17.0 v1.20.0 Symfony polyfill for intl...
symfony/polyfill-intl-icu          v1.17.0 v1.20.0 Symfony polyfill for intl...
symfony/polyfill-intl-idn          v1.17.0 v1.20.0 Symfony polyfill for intl...
symfony/polyfill-intl-normalizer   v1.17.0 v1.20.0 Symfony polyfill for intl...
symfony/polyfill-mbstring          v1.17.0 v1.20.0 Symfony polyfill for the ...
symfony/polyfill-php72             v1.17.0 v1.20.0 Symfony polyfill backport...
symfony/polyfill-php73             v1.17.0 v1.20.0 Symfony polyfill backport...
symfony/process                    v4.4.8  v4.4.17 Symfony Process Component
symfony/profiler-pack              v1.0.4  v1.0.5  A pack for the Symfony we...
symfony/property-access            v4.4.8  v4.4.17 Symfony PropertyAccess Co...
symfony/property-info              v4.4.8  v4.4.17 Symfony Property Info Com...
symfony/proxy-manager-bridge       v4.4.8  v4.4.17 Symfony ProxyManager Bridge
symfony/routing                    v4.4.8  v4.4.17 Symfony Routing Component
symfony/security-acl               v3.0.4  v3.1.0  Symfony Security Componen...
symfony/security-bundle            v4.4.8  v4.4.17 Symfony SecurityBundle
symfony/security-core              v4.4.8  v4.4.17 Symfony Security Componen...
symfony/security-csrf              v4.4.8  v4.4.17 Symfony Security Componen...
symfony/security-guard             v4.4.8  v4.4.17 Symfony Security Componen...
symfony/security-http              v4.4.8  v4.4.17 Symfony Security Componen...
symfony/serializer                 v4.4.8  v4.4.17 Symfony Serializer Component
symfony/serializer-pack            v1.0.3  v1.0.4  A pack for the Symfony se...
symfony/service-contracts          v2.0.1  v2.2.0  Generic abstractions rela...
symfony/stopwatch                  v4.4.8  v4.4.17 Symfony Stopwatch Component
symfony/string                     v5.0.8  Symfony String component
symfony/swiftmailer-bundle         v3.4.0  v3.5.1  Symfony SwiftmailerBundle
symfony/templating                 v4.4.8  v4.4.17 Symfony Templating Component
symfony/translation                v4.4.8  v4.4.17 Symfony Translation Compo...
symfony/translation-contracts      v2.0.1  v2.3.0  Generic abstractions rela...
symfony/twig-bridge                v4.4.8  v4.4.17 Symfony Twig Bridge
symfony/twig-bundle                v4.4.8  v4.4.17 Symfony TwigBundle
symfony/validator                  v4.4.8  v4.4.17 Symfony Validator Component
symfony/var-dumper                 v4.4.8  v4.4.17 Symfony mechanism for exp...
symfony/var-exporter               v4.4.8  v4.4.17 A blend of var_export() +...
symfony/web-link                   v4.4.8  v4.4.17 Symfony WebLink Component
symfony/web-profiler-bundle        v4.4.8  v4.4.17 Symfony WebProfilerBundle
symfony/webpack-encore-bundle      v1.7.3  v1.8.0  Integration with your Sym...
symfony/workflow                   v4.4.8  v4.4.17 Symfony Workflow Component
symfony/yaml                       v4.4.8  v4.4.17 Symfony Yaml Component

PHP version

$ php -v
PHP 7.4.11 (cli) (built: Oct 13 2020 10:09:45) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.11, Copyright (c), by Zend Technologies

Subject

Minimal repository with the bug

protected function configureListFields(ListMapper $listMapper) {

    $listMapper->add('id');
    $listMapper->add('object1', null, [
        'label' => 'Name',
        'associated_property' => function (Object1 $object) {
            return $object->translate()->getName();
        },
    ]);

Steps to reproduce

Sort By Name

Expected results

To sort

Actual results

Crash with Object of class Closure could not be converted to string

`Error: Object of class Closure could not be converted to string

at /app/vendor/sonata-project/doctrine-orm-admin-bundle/src/Datagrid/ProxyQuery.php:233 at Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery->setSortBy(array(array('fieldName' => 'object1')), array('fieldName' => object(Closure))) (/app/vendor/sonata-project/admin-bundle/src/Datagrid/Datagrid.php:165) at Sonata\AdminBundle\Datagrid\Datagrid->buildPager() (/app/vendor/sonata-project/admin-bundle/src/Datagrid/Datagrid.php:292) at Sonata\AdminBundle\Datagrid\Datagrid->getForm() (/app/vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php:135) at Sonata\AdminBundle\Controller\CRUDController->listAction() (/app/vendor/symfony/http-kernel/HttpKernel.php:158) at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1) (/app/vendor/symfony/http-kernel/HttpKernel.php:80) at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true) (/app/vendor/symfony/http-kernel/Kernel.php:201) at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) (/app/public/index.php:83) `

VincentLanglet commented 3 years ago

This is not a bug. The associated_property is a property path to retrieve the "string" representation of the collection element ; not a closure.

Nayden-TaylorAndHart commented 3 years ago

Thanks, Documentaion .associated_property (o): property path to retrieve the “string” representation of the collection element, or a closure with the element as argument and return a string.

VincentLanglet commented 3 years ago

I didn't look at the doc, I looked at the code: https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Admin/BaseFieldDescription.php#L41

associated_property is used here: https://github.com/sonata-project/SonataAdminBundle/blob/57ee852cecaddb25b6502186c1cf84c13945d8f9/src/Datagrid/ListMapper.php#L98

I don't think it's possible to support a closure.

I'll update the doc ; thanks for the report !

Nayden-TaylorAndHart commented 3 years ago

np :)

franmomu commented 3 years ago

Now I re-read this, the problem is about sorting, associated_property admits a closure for visualizing the data, but you cannot sort by that (because it's a "calculated" field).

VincentLanglet commented 3 years ago

Now I re-read this, the problem is about sorting, associated_property admits a closure for visualizing the data, but you cannot sort by that (because it's a "calculated" field).

Then the sort should be disable, how is it decided if the column can be sorted or not ?