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

Composite key gives error. Error: Invalid PathExpression. Must be a StateFieldPathExpression. #6912

Closed lukepass closed 3 years ago

lukepass commented 3 years ago

Environment

Symfony 4.4

Sonata packages

show

``` $ composer show --latest 'sonata-project/*' sonata-project/admin-bundle 3.79.0 3.79.0 The missing Symfony Admin Generator sonata-project/block-bundle 3.21.0 3.21.0 Symfony SonataBlockBundle sonata-project/cache 2.1.1 2.1.1 Cache library sonata-project/doctrine-extensions 1.10.1 1.10.1 Doctrine2 behavioral extensions sonata-project/doctrine-orm-admin-bundle 3.24.0 3.24.0 Integrate Doctrine ORM into the Sonata... sonata-project/exporter 2.4.1 2.4.1 Lightweight Exporter library sonata-project/form-extensions 1.6.0 1.6.0 Symfony form extensions sonata-project/media-bundle 3.28.0 3.28.0 Symfony SonataMediaBundle sonata-project/twig-extensions 1.4.1 1.4.1 Sonata twig extensions ```

Symfony packages

show

``` $ composer show --latest 'symfony/*' symfony/apache-pack v1.0.1 v1.0.1 A pack for Apache support in Symfony symfony/asset v4.4.20 v5.2.4 Manages URL generation and versioning of w... symfony/browser-kit v4.4.20 v5.2.4 Simulates the behavior of a web browser, a... symfony/cache v4.4.20 v5.2.4 Provides an extended PSR-6, PSR-16 (and ta... symfony/cache-contracts v2.2.0 v2.2.0 Generic abstractions related to caching symfony/config v4.4.20 v5.2.4 Helps you find, load, combine, autofill an... symfony/console v4.4.20 v5.2.4 Eases the creation of beautiful and testab... symfony/css-selector v4.4.20 v5.2.4 Converts CSS selectors to XPath expressions symfony/debug v4.4.20 v4.4.20 Provides tools to ease debugging PHP code symfony/debug-bundle v4.4.20 v5.2.4 Provides a tight integration of the Symfon... symfony/dependency-injection v4.4.20 v5.2.4 Allows you to standardize and centralize t... symfony/deprecation-contracts v2.2.0 v2.2.0 A generic function and convention to trigg... symfony/doctrine-bridge v4.4.20 v5.2.4 Provides integration for Doctrine with var... symfony/dom-crawler v4.4.20 v5.2.4 Eases DOM navigation for HTML and XML docu... symfony/dotenv v4.4.20 v5.2.4 Registers environment variables from a .en... symfony/error-handler v4.4.20 v5.2.4 Provides tools to manage errors and ease d... symfony/event-dispatcher v4.4.20 v5.2.4 Provides tools that allow your application... symfony/event-dispatcher-contracts v1.1.9 v2.2.0 Generic abstractions related to dispatchin... symfony/expression-language v4.4.20 v5.2.4 Provides an engine that can compile and ev... symfony/filesystem v4.4.20 v5.2.4 Provides basic utilities for the filesystem symfony/finder v4.4.20 v5.2.4 Finds files and directories via an intuiti... symfony/flex v1.12.2 v1.12.2 Composer plugin for Symfony symfony/form v4.4.20 v5.2.4 Allows to easily create, process and reuse... symfony/framework-bundle v4.4.20 v5.2.4 Provides a tight integration between Symfo... symfony/google-mailer v4.4.20 v5.2.4 Symfony Google Mailer Bridge symfony/http-client v4.4.20 v5.2.4 Provides powerful methods to fetch HTTP re... symfony/http-client-contracts v2.3.1 v2.3.1 Generic abstractions related to HTTP clients symfony/http-foundation v4.4.20 v5.2.4 Defines an object-oriented layer for the H... symfony/http-kernel v4.4.20 v5.2.4 Provides a structured process for converti... symfony/inflector v4.4.20 v5.2.4 Converts words between their singular and ... symfony/intl v4.4.20 v5.2.4 Provides a PHP replacement layer for the C... symfony/mailer v4.4.20 v5.2.4 Helps sending emails symfony/maker-bundle v1.29.1 v1.29.1 Symfony Maker helps you create empty comma... symfony/messenger v4.4.20 v5.2.4 Helps applications send and receive messag... symfony/mime v4.4.20 v5.2.4 Allows manipulating MIME messages symfony/monolog-bridge v4.4.20 v5.2.4 Provides integration for Monolog with vari... symfony/monolog-bundle v3.6.0 v3.6.0 Symfony MonologBundle symfony/options-resolver v4.4.20 v5.2.4 Provides an improved replacement for the a... symfony/phpunit-bridge v5.2.4 v5.2.4 Provides utilities for PHPUnit, especially... symfony/polyfill-intl-grapheme v1.22.1 v1.22.1 Symfony polyfill for intl's grapheme_* fun... symfony/polyfill-intl-icu v1.22.1 v1.22.1 Symfony polyfill for intl's ICU-related da... symfony/polyfill-intl-idn v1.22.1 v1.22.1 Symfony polyfill for intl's idn_to_ascii a... symfony/polyfill-intl-normalizer v1.22.1 v1.22.1 Symfony polyfill for intl's Normalizer cla... symfony/polyfill-mbstring v1.22.1 v1.22.1 Symfony polyfill for the Mbstring extension symfony/polyfill-php72 v1.22.1 v1.22.1 Symfony polyfill backporting some PHP 7.2+... symfony/polyfill-php73 v1.22.1 v1.22.1 Symfony polyfill backporting some PHP 7.3+... symfony/polyfill-php80 v1.22.1 v1.22.1 Symfony polyfill backporting some PHP 8.0+... symfony/process v4.4.20 v5.2.4 Executes commands in sub-processes symfony/property-access v4.4.20 v5.2.4 Provides functions to read and write from/... symfony/property-info v4.4.20 v5.2.4 Extracts information about PHP class' prop... symfony/routing v4.4.20 v5.2.4 Maps an HTTP request to a set of configura... symfony/security-acl v3.1.1 v3.1.1 Symfony Security Component - ACL (Access C... symfony/security-bundle v4.4.20 v5.2.4 Provides a tight integration of the Securi... symfony/security-core v4.4.20 v5.2.4 Symfony Security Component - Core Library symfony/security-csrf v4.4.20 v5.2.4 Symfony Security Component - CSRF Library symfony/security-guard v4.4.20 v5.2.4 Symfony Security Component - Guard symfony/security-http v4.4.20 v5.2.4 Symfony Security Component - HTTP Integration symfony/serializer v4.4.20 v5.2.4 Handles serializing and deserializing data... symfony/service-contracts v2.2.0 v2.2.0 Generic abstractions related to writing se... symfony/stopwatch v4.4.20 v5.2.4 Provides a way to profile code symfony/string v5.2.4 v5.2.4 Provides an object-oriented API to strings... symfony/templating v4.4.20 v5.2.4 Provides all the tools needed to build any... symfony/translation v4.4.20 v5.2.4 Provides tools to internationalize your ap... symfony/translation-contracts v2.3.0 v2.3.0 Generic abstractions related to translation symfony/twig-bridge v4.4.20 v5.2.4 Provides integration for Twig with various... symfony/twig-bundle v4.4.20 v5.2.4 Provides a tight integration of Twig into ... symfony/validator v4.4.20 v5.2.4 Provides tools to validate values symfony/var-dumper v4.4.20 v5.2.4 Provides mechanisms for walking through an... symfony/var-exporter v4.4.20 v5.2.4 Allows exporting any serializable PHP data... symfony/web-link v4.4.20 v5.2.4 Manages links between resources symfony/web-profiler-bundle v4.4.20 v5.2.4 Provides a development tool that gives det... symfony/webpack-encore-bundle v1.11.1 v1.11.1 Integration with your Symfony app & Webpac... symfony/yaml v4.4.20 v5.2.4 Loads and dumps YAML files ```

PHP version

$ php -v
PHP 7.4.15 (cli) (built: Feb 19 2021 15:42:28) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.15, Copyright (c), by Zend Technologies
    with Xdebug v3.0.2, Copyright (c) 2002-2021, by Derick Rethans

Subject

When using entities which have a composite key every listing page will give the following error:

[Semantical Error] line 0, col 31 near 'student, :concat_separator,': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

I saw that this bug should have been fixed here: https://github.com/sonata-project/SonataDoctrineORMAdminBundle/issues/834

The class is very simple:

/**
 * @ORM\Entity(repositoryClass=StudentChapterRepository::class)
 */
class StudentChapter
{
    use TimestampableTrait;

    /**
     * @var Student
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity=Student::class)
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     */
    private $student;

    /**
     * @var Chapter
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity=Chapter::class)
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     */
    private $chapter;

    public function __construct(Student $student, Chapter $chapter)
    {
        $this->student = $student;
        $this->chapter = $chapter;
    }

    public function getStudent(): ?Student
    {
        return $this->student;
    }

    public function getChapter(): ?Chapter
    {
        return $this->chapter;
    }

}

And also the admin:

protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
{
    $datagridMapper
        ->add('student')
        ->add('chapter')
    ;
}

protected function configureListFields(ListMapper $listMapper): void
{
    $listMapper
        ->addIdentifier('student')
        ->addIdentifier('chapter')
        ->add('_action', null, [
            'actions' => [
                'show' => [],
                'edit' => [],
                'delete' => [],
            ],
        ])
    ;
}

Thanks.

Minimal repository with the bug

See above.

Steps to reproduce

Create a class with a composite key and an admin. The listing will give an error.

Expected results

Actual results

VincentLanglet commented 3 years ago

Can you update to the latest version ?

Some release are talking about composite keys like https://github.com/sonata-project/SonataDoctrineORMAdminBundle/releases/tag/3.28.0

It would be also be interesting that you tried the last commit of the stable branch (not even released) because there is this PR https://github.com/sonata-project/SonataDoctrineORMAdminBundle/pull/1319 which removed all the use of Distinct in favor of groupBy ; since the link you gave have a comment linking to the introduction of distinct https://github.com/sonata-project/SonataDoctrineORMAdminBundle/issues/834#issuecomment-393166656

lukepass commented 3 years ago

Thanks for your quick answer @VincentLanglet , unfortunately I cannot update to the 3.28 because it requires PHP 7.3, although I have the 7.4 in my dev environment I cannot update the server production.

https://packagist.org/packages/sonata-project/doctrine-orm-admin-bundle#3.24.0 ==> this is the last working with PHP 7.2

Any other solution? Would it be possible to backport it to PHP 7.2?

Thanks.

VincentLanglet commented 3 years ago

Our policy is to follow https://www.php.net/supported-versions.php. This allow us to use the new php functionnality.

I would recommend to try the latest version in your dev environment in order to check if it fix the issue. If yes, you'll have two solutions:

Be aware that if it doesn't fix the bug, the bugfix will be introduce in 3.31 version or later. So for php 7.3+ only. :/

VincentLanglet commented 3 years ago

Closing since it's supposed to be fixed in the last DoctrineORM versions. At least the concat_operator is not use anymore. https://github.com/sonata-project/SonataDoctrineORMAdminBundle/commit/fa01b05e23a6a4ea220adca510c1e55ca45ea8f0