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

Admin with children forms does not associate entities #6426

Closed ossinkine closed 3 years ago

ossinkine commented 4 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.76.0 3.76.0 The missing Symfony Admin Generator
sonata-project/block-bundle              4.3.0  4.3.0  Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/doctrine-extensions       1.9.1  1.9.1  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.23.0 3.23.0 Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.3.0  2.3.0  Lightweight Exporter library
sonata-project/form-extensions           1.6.0  1.6.0  Symfony form extensions
sonata-project/twig-extensions           1.4.1  1.4.1  Sonata twig extensions

Symfony packages

$ composer show --latest 'symfony/*'
symfony/asset                      v4.4.14 v4.4.14 Symfony Asset Component
symfony/cache                      v4.4.14 v4.4.14 Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v2.2.0  v2.2.0  Generic abstractions related to caching
symfony/config                     v4.4.14 v4.4.14 Symfony Config Component
symfony/console                    v4.4.14 v4.4.14 Symfony Console Component
symfony/debug                      v4.4.14 v4.4.14 Symfony Debug Component
symfony/dependency-injection       v4.4.14 v4.4.14 Symfony DependencyInjection Component
symfony/doctrine-bridge            v4.4.14 v4.4.14 Symfony Doctrine Bridge
symfony/dotenv                     v4.4.14 v4.4.14 Registers environment variables from a .env file
symfony/error-handler              v4.4.14 v4.4.14 Symfony ErrorHandler Component
symfony/event-dispatcher           v4.4.14 v4.4.14 Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.9  v2.2.0  Generic abstractions related to dispatching event
symfony/expression-language        v4.4.14 v4.4.14 Symfony ExpressionLanguage Component
symfony/filesystem                 v4.4.14 v4.4.14 Symfony Filesystem Component
symfony/finder                     v4.4.14 v4.4.14 Symfony Finder Component
symfony/flex                       v1.9.4  v1.9.4  Composer plugin for Symfony
symfony/form                       v4.4.14 v4.4.14 Symfony Form Component
symfony/framework-bundle           v4.4.14 v4.4.14 Symfony FrameworkBundle
symfony/http-client-contracts      v2.2.0  v2.2.0  Generic abstractions related to HTTP clients
symfony/http-foundation            v4.4.14 v4.4.14 Symfony HttpFoundation Component
symfony/http-kernel                v4.4.14 v4.4.14 Symfony HttpKernel Component
symfony/inflector                  v4.4.14 v4.4.14 Symfony Inflector Component
symfony/intl                       v4.4.14 v4.4.14 A PHP replacement layer for the C intl extension that includes additional data from the ICU libr...
symfony/maker-bundle               v1.21.1 v1.21.1 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you ...
symfony/mime                       v4.4.14 v4.4.14 A library to manipulate MIME messages
symfony/options-resolver           v4.4.14 v4.4.14 Symfony OptionsResolver Component
symfony/polyfill-intl-grapheme     v1.18.1 v1.18.1 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.18.1 v1.18.1 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.18.1 v1.18.1 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.18.1 v1.18.1 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.18.1 v1.18.1 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.18.1 v1.18.1 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.18.1 v1.18.1 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80             v1.18.1 v1.18.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/property-access            v4.4.14 v4.4.14 Symfony PropertyAccess Component
symfony/routing                    v4.4.14 v4.4.14 Symfony Routing Component
symfony/security-acl               v3.1.0  v3.1.0  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.4.14 v4.4.14 Symfony SecurityBundle
symfony/security-core              v4.4.14 v4.4.14 Symfony Security Component - Core Library
symfony/security-csrf              v4.4.14 v4.4.14 Symfony Security Component - CSRF Library
symfony/security-guard             v4.4.14 v4.4.14 Symfony Security Component - Guard
symfony/security-http              v4.4.14 v4.4.14 Symfony Security Component - HTTP Integration
symfony/service-contracts          v2.2.0  v2.2.0  Generic abstractions related to writing services
symfony/string                     v5.1.6  v5.1.6  Symfony String component
symfony/translation                v4.4.14 v4.4.14 Symfony Translation Component
symfony/translation-contracts      v2.2.0  v2.2.0  Generic abstractions related to translation
symfony/twig-bridge                v4.4.14 v4.4.14 Symfony Twig Bridge
symfony/twig-bundle                v4.4.14 v4.4.14 Symfony TwigBundle
symfony/validator                  v4.4.14 v4.4.14 Symfony Validator Component
symfony/var-dumper                 v4.4.14 v4.4.14 Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.4.14 v4.4.14 A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/yaml                       v4.4.14 v4.4.14 Symfony Yaml Component

PHP version

$ php -v
PHP 7.4.10 (cli) (built: Sep  3 2020 19:59:29) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.10, Copyright (c), by Zend Technologies
    with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans

Subject

After update sonata-project/admin-bundle 3.71.1 to 3.75.0 I've got a regression issue. Admin which has children forms does not associate the entities. After couple of hours of debugging I've discovered the issue introduced in #6171.

Steps to reproduce

There is a reproducer here https://github.com/ossinkine/sonata-admin-reproducer-6426

Expected results

Open parent object admin in the reproducer and try to create a parent object with a child object and save. No errors expected.

Actual results

There is an error SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: child_object.parent_object_id because the child object does not associated with a parent object.

ossinkine commented 4 years ago

cc @VincentLanglet

VincentLanglet commented 4 years ago

Thanks for the reproducer, I will try to find some time to look at it.

VincentLanglet commented 4 years ago

It tooks me some hours to debug this.

Let's say we have the parent has the collection:

[0] => Child

When we add one in the admin, by using addInstance in the Admin Type (with reference false), we had

[0] => Child
[1] => New Child

Then when submitting

[0] => Child
// The key 1 is removed since it's not the same reference than the NewChild added.
[2] => New Child

And the form error was affected to the key 1.

But with reference true, when submitting we have

[0] => Child
[1] => New Child

So there is no issue with the form error, and we can use addInstance.

It important to notice that using reference => false in your case, fix your issue: I can create parent with child.

So if reference => false is used, setObject seems to be the correct method. And if reference => true is used, addInstance seems to be the correct method.

Now the question is: Does the AdminType is able to know the option passed to the CollectionType ?

VincentLanglet commented 4 years ago

I started a draft PR https://github.com/sonata-project/SonataAdminBundle/pull/6438

I will also need to create a PR on our persistence bundle.

VincentLanglet commented 3 years ago

Fixed by https://github.com/sonata-project/SonataAdminBundle/pull/6438