sonata-project / SonataDoctrineORMAdminBundle

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

Unable to export admin list when sorted by a translated property #1701

Closed Cenadros closed 1 year ago

Cenadros commented 1 year ago

Environment

Sonata packages

show

``` $ composer show --latest 'sonata-project/*' sonata-project/admin-bundle 4.20.0 4.20.0 The missing Symfony Admin Generator sonata-project/block-bundle 4.19.0 4.19.0 Symfony SonataBlockBundle sonata-project/cache 2.2.0 2.2.0 Cache library sonata-project/doctrine-extensions 1.18.1 1.18.1 Doctrine2 behavioral extensions sonata-project/doctrine-orm-admin-bundle 4.8.0 4.8.0 Integrate Doctrine ORM into the SonataAdminBundle sonata-project/exporter 2.13.0 2.13.0 Lightweight Exporter library sonata-project/form-extensions 1.18.0 1.18.0 Symfony form extensions sonata-project/media-bundle 4.5.1 4.5.1 Symfony SonataMediaBundle sonata-project/twig-extensions 1.12.0 1.12.0 Sonata twig extensions sonata-project/user-bundle 5.4.0 5.4.0 Symfony SonataUserBundle ```

Symfony packages

show

``` $ composer show --latest 'symfony/*' symfony/asset v4.4.46 v5.4.13 Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files symfony/browser-kit v4.4.44 v5.4.11 Simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically symfony/cache v4.4.46 v5.4.13 Provides extended PSR-6, PSR-16 (and tags) implementations symfony/cache-contracts v2.5.2 v2.5.2 Generic abstractions related to caching symfony/config v4.4.44 v5.4.11 Helps you find, load, combine, autofill and validate configuration values of any kind symfony/console v4.4.47 v5.4.14 Eases the creation of beautiful and testable command line interfaces symfony/debug v4.4.44 v4.4.44 Provides tools to ease debugging PHP code symfony/debug-bundle v4.4.37 v5.4.11 Provides a tight integration of the Symfony VarDumper component and the ServerLogCommand from MonologBridge into the Symfony full-stack framework symfony/dependency-injection v4.4.44 v5.4.13 Allows you to standardize and centralize the way objects are constructed in your application symfony/deprecation-contracts v2.5.2 v2.5.2 A generic function and convention to trigger deprecation notices symfony/doctrine-bridge v4.4.46 v5.4.14 Provides integration for Doctrine with various Symfony components symfony/dom-crawler v4.4.45 v5.4.12 Eases DOM navigation for HTML and XML documents symfony/error-handler v4.4.44 v5.4.14 Provides tools to manage errors and ease debugging PHP code symfony/event-dispatcher v4.4.44 v5.4.9 Provides tools that allow your application components to communicate with each other by dispatching events and listening to them symfony/event-dispatcher-contracts v1.1.13 v2.5.2 Generic abstractions related to dispatching event symfony/expression-language v4.4.47 v5.4.14 Provides an engine that can compile and evaluate expressions symfony/filesystem v4.4.42 v5.4.13 Provides basic utilities for the filesystem symfony/finder v4.4.44 v5.4.11 Finds files and directories via an intuitive fluent interface symfony/flex v1.19.3 v1.19.3 Composer plugin for Symfony symfony/form v4.4.46 v5.4.13 Allows to easily create, process and reuse HTML forms symfony/framework-bundle v4.4.47 v5.4.14 Provides a tight integration between Symfony components and the Symfony full-stack framework symfony/http-client v4.4.47 v5.4.14 Provides powerful methods to fetch HTTP resources synchronously or asynchronously symfony/http-client-contracts v2.5.2 v2.5.2 Generic abstractions related to HTTP clients symfony/http-foundation v4.4.47 v5.4.14 Defines an object-oriented layer for the HTTP specification symfony/http-kernel v4.4.47 v5.4.14 Provides a structured process for converting a Request into a Response symfony/inflector v4.4.44 v5.4.11 Converts words between their singular and plural forms (English only) symfony/intl v4.4.47 v5.4.14 Provides a PHP replacement layer for the C intl extension that includes additional data from the ICU library symfony/mailer v4.4.46 v5.4.13 Helps sending emails symfony/maker-bundle v1.38.0 v1.43.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code. symfony/mime v4.4.47 v5.4.14 Allows manipulating MIME messages symfony/monolog-bridge v4.4.43 v5.4.10 Provides integration for Monolog with various Symfony components symfony/monolog-bundle v3.8.0 v3.8.0 Symfony MonologBundle symfony/options-resolver v4.4.44 v5.4.11 Provides an improved replacement for the array_replace PHP function symfony/polyfill-ctype v1.26.0 v1.26.0 Symfony polyfill for ctype functions symfony/polyfill-iconv v1.26.0 v1.26.0 Symfony polyfill for the Iconv extension symfony/polyfill-intl-grapheme v1.26.0 v1.26.0 Symfony polyfill for intl's grapheme_* functions symfony/polyfill-intl-icu v1.26.0 v1.26.0 Symfony polyfill for intl's ICU-related data and classes symfony/polyfill-intl-idn v1.26.0 v1.26.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions symfony/polyfill-intl-normalizer v1.26.0 v1.26.0 Symfony polyfill for intl's Normalizer class and related functions symfony/polyfill-mbstring v1.26.0 v1.26.0 Symfony polyfill for the Mbstring extension symfony/polyfill-php72 v1.26.0 v1.26.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions symfony/polyfill-php73 v1.26.0 v1.26.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions symfony/polyfill-php80 v1.26.0 v1.26.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions symfony/polyfill-php81 v1.26.0 v1.26.0 Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions symfony/process v4.4.44 v5.4.11 Executes commands in sub-processes symfony/property-access v4.4.44 v5.4.11 Provides functions to read and write from/to an object or array using a simple string notation symfony/routing v4.4.44 v5.4.11 Maps an HTTP request to a set of configuration variables symfony/security-acl v3.3.2 v3.3.2 Symfony Security Component - ACL (Access Control List) symfony/security-bundle v4.4.44 v5.4.11 Provides a tight integration of the Security component into the Symfony full-stack framework symfony/security-core v4.4.47 v5.4.14 Symfony Security Component - Core Library symfony/security-csrf v4.4.37 v5.4.11 Symfony Security Component - CSRF Library symfony/security-guard v4.4.46 v5.4.13 Symfony Security Component - Guard symfony/security-http v4.4.44 v5.4.13 Symfony Security Component - HTTP Integration symfony/serializer v4.4.47 v5.4.14 Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON. symfony/service-contracts v2.5.2 v2.5.2 Generic abstractions related to writing services symfony/stopwatch v4.4.46 v5.4.13 Provides a way to profile code symfony/string v5.4.14 v5.4.14 Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way symfony/swiftmailer-bundle v3.5.4 v3.5.4 Symfony SwiftmailerBundle Package symfony/swiftmailer-bundle is abandoned, you should avoid using it. Use symfony/mailer instead. symfony/translation v4.4.47 v5.4.14 Provides tools to internationalize your application symfony/translation-contracts v2.5.2 v2.5.2 Generic abstractions related to translation symfony/twig-bridge v4.4.45 v5.4.14 Provides integration for Twig with various Symfony components symfony/twig-bundle v4.4.41 v5.4.8 Provides a tight integration of Twig into the Symfony full-stack framework symfony/validator v4.4.47 v5.4.14 Provides tools to validate values symfony/var-dumper v4.4.47 v5.4.14 Provides mechanisms for walking through any arbitrary PHP variable symfony/var-exporter v4.4.43 v5.4.10 Allows exporting any serializable PHP data structure to plain PHP code symfony/web-profiler-bundle v4.4.47 v5.4.14 Provides a development tool that gives detailed information about the execution of any request symfony/yaml v4.4.45 v5.4.14 Loads and dumps YAML files ```

PHP version

$ php -v
PHP 8.1.3 (cli) (built: Feb 18 2022 19:37:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies
    with Xdebug v3.2.0RC1, Copyright (c) 2002-2022, by Derick Rethans

Subject

In #1639 a fix was included in Sonata\DoctrineORMAdminBundle\Exporter\DataSource.php for exporting when sorting on ManyToOne column that breaks exporting when a column is from a translation entity and is sorted by it.

With this change the export fails with the following error when trying to export an entity Entity associated with its translation EntityTranslation

Iterate with fetch join in class AppBundle\Entity\EntityTranslation using association translatable not allowed.

Before this change the export would work although it was not exported sorted by the property.

VincentLanglet commented 1 year ago

Hi @Cenadros, can you give us the working and/or non-working query ?

cc @willemverspyck, does it fully works for you ?

willemverspyck commented 1 year ago

Hi @VincentLanglet, @Cenadros,

For me the export still works when sorting on ManyToOne column. Maybe you can send minimal repository with the bug or some example of your entities?

Cenadros commented 1 year ago

Hi @VincentLanglet @willemverspyck ,

The working query is:

SELECT DISTINCT o FROM AppBundle\Entity\Contact o LEFT JOIN o.translations s_translations WHERE o.type = :type ORDER BY s_translations.name ASC, o.id ASC

And the non-working query is:

SELECT DISTINCT o, s_translations FROM AppBundle\Entity\Contact o LEFT JOIN o.translations s_translations WHERE o.type = :type ORDER BY s_translations.name ASC, o.id ASC

An example of the entities would be this:

Contact:

show

``` use Doctrine\ORM\Mapping as ORM; use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface; use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait; class Contact implements TranslatableInterface { use TranslatableTrait; /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ protected $id; public function getId(): ?int { return $this->id; } public function getName(): ?string { return $this->translate()->getName(); } } ```

ContactTranslation:

show

``` use Doctrine\ORM\Mapping as ORM; use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface; use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait; use Symfony\Component\Validator\Constraints as Assert; class ContactTranslation implements TranslationInterface { use TranslationTrait; /** * @Assert\NotBlank * @ORM\Column(type="string") */ protected $name; /** * @var int * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; public function setName(?string $name): self { $this->name = $name; return $this; } public function getName(): ?string { return $this->name; } } ```

ContactAdmin:

show

``` use A2lix\TranslationFormBundle\Form\Type\TranslationsType; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Datagrid\DatagridInterface; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Form\FormMapper; use Symfony\Component\Validator\Constraints as Assert; class ContactAdmin extends AbstractAdmin { protected function configureDefaultSortValues(array &$sortValues): void { $sortValues[DatagridInterface::SORT_ORDER] = 'ASC'; $sortValues[DatagridInterface::SORT_BY] = 'name'; } protected function configureListFields(ListMapper $list): void { $list ->addIdentifier('name', null, [ 'sortable' => true, 'sort_field_mapping' => [ 'fieldName' => 'name', ], 'sort_parent_association_mappings' => [[ 'fieldName' => 'translations', ]], ]) ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [ 'actions' => [ 'delete' => [], ], ]); } protected function configureFormFields(FormMapper $form): void { $form ->with('Contact', [ 'box_class' => 'box box-solid box-primary', ]) ->add('translations', TranslationsType::class, [ 'label' => false, 'fields' => [ 'name' => [], ], 'constraints' => [ new Assert\Valid(), ], ]) ->end(); } } ```

The moment there is a default sort by a translatable propery or you order the admin list by that property and then try to export, it gives the error.

VincentLanglet commented 1 year ago

The working query is:

SELECT DISTINCT o FROM AppBundle\Entity\Contact o LEFT JOIN o.translations s_translations WHERE o.type = :type ORDER BY s_translations.name ASC, o.id ASC

And the non-working query is:

SELECT DISTINCT o, s_translations FROM AppBundle\Entity\Contact o LEFT JOIN o.translations s_translations WHERE o.type = :type ORDER BY s_translations.name ASC, o.id ASC

So the only diff is the that that s_translations was added to the select.

What is weird is the fact it's opposed to the issue https://github.com/sonata-project/SonataDoctrineORMAdminBundle/issues/1586 where

SELECT DISTINCT o FROM App\Entity\Entity o INNER JOIN o.cost cost ORDER BY cost.reference ASC, o.id ASC

wasn't working and

SELECT DISTINCT o, cost FROM App\Entity\Entity o INNER JOIN o.cost cost ORDER BY cost.reference ASC, o.id ASC

was.

willemverspyck commented 1 year ago

I think I understand the problem. For me it currently still works with:

SELECT DISTINCT o, cost FROM App\Entity\Entity o INNER JOIN o.cost cost ORDER BY cost.reference ASC, o.id ASC

And "cost" is still added to the select, but @Cenadros uses the field "name" in "configureListFields". And I have "cost.amount" there for example. This part of code checks for a dot in the field name (I couldn't find any other way that moment):

https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/b0c2cbddd33565c3447b7fc1d28955ae020e863b/src/Exporter/DataSource.php#L49

Can you maybe confirm that @Cenadros with a dump of:

https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/b0c2cbddd33565c3447b7fc1d28955ae020e863b/src/Exporter/DataSource.php#L44

Or can you maybe try "translations.name" instead of "name" in "configureListFields" and remove "sort_field_mapping" and "sort_parent_association_mappings"?

Otherwise maybe this can be fixed somehow with the "sort_field_mapping" and "sort_parent_association_mappings" as you have defined them 🤔

Cenadros commented 1 year ago

@willemverspyck I've made the dump and $sortBy has the value of s_translations.name.

Also tried to set translations.name instead of name and removing the sort_field_mapping and sort_parent_association_mappings and got the following error when trying to access de admin list:

An exception has been thrown during the rendering of a template ("Cannot access property "translations.name" in class "AppBundle\Entity\Contact".").
jordisala1991 commented 1 year ago

The difference is that translations is OneToMany probably?

jordisala1991 commented 1 year ago

I will try to cover all cases here: https://github.com/sonata-project/SonataDoctrineORMAdminBundle/pull/1705

to see where does it fail.