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

Losing relation information on edit with sonata_type_model_autocomplete #4434

Closed JeremyDulon closed 4 years ago

JeremyDulon commented 7 years ago

Environment

Windows 10, WampServer

Sonata packages

sonata-project/admin-bundle              3.13.0 The missing Symfony Admin ...
sonata-project/block-bundle              3.3.1  Symfony SonataBlockBundle
sonata-project/cache                     1.0.7  Cache library
sonata-project/core-bundle               3.2.0  Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.2.1  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.0.2  Doctrine2 behavioral exten...
sonata-project/doctrine-orm-admin-bundle 3.1.3  Symfony Sonata / Integrate...
sonata-project/easy-extends-bundle       2.1.10 Symfony SonataEasyExtendsB...
sonata-project/exporter                  1.7.1  Lightweight Exporter library
sonata-project/formatter-bundle          3.2.0  Symfony SonataFormatterBundle
sonata-project/google-authenticator      1.0.2  Library to integrate Googl...
sonata-project/jquery-bundle             1.10.2 Symfony SonatajQueryBundle
sonata-project/markitup-bundle           2.1.0  Symfony SonataMarkItUpBundle
sonata-project/user-bundle               3.2.1  Symfony SonataUserBundle

Symfony packages

symfony/assetic-bundle     dev-master 0241b13 Integrates Assetic into Symf...
symfony/monolog-bundle     v2.12.1            Symfony MonologBundle
symfony/polyfill-apcu      v1.3.0             Symfony polyfill backporting...
symfony/polyfill-intl-icu  v1.3.0             Symfony polyfill for intl's ...
symfony/polyfill-mbstring  v1.3.0             Symfony polyfill for the Mbs...
symfony/polyfill-php54     v1.3.0             Symfony polyfill backporting...
symfony/polyfill-php55     v1.3.0             Symfony polyfill backporting...
symfony/polyfill-php56     v1.3.0             Symfony polyfill backporting...
symfony/polyfill-php70     v1.3.0             Symfony polyfill backporting...
symfony/polyfill-util      v1.3.0             Symfony utilities for portab...
symfony/security-acl       v3.0.0             Symfony Security Component -...
symfony/swiftmailer-bundle v2.5.3             Symfony SwiftmailerBundle
symfony/symfony            v2.8.18            The Symfony PHP framework

PHP version

5.6.25

Subject

On an admin class WatchText, I got a relationship One to Many relationship WatchTextUsers, with the type "sonata_type_collection". In this embedded form, I got three fields, a User (where I use the "sonata_type_model_autocomplete" type), and two text fields.

Steps to reproduce

When you create a WatchText entity, you can add as many User as you want, and filling the following two fields according.

So this works quite well, you can then save all your entity, and in the database the User is linked with the WatchTextUser.

But, when you edit an existing WatchText, even though you can see the existing WatchTextUsers, with the three fields filled (even the autocomplete, with the property of my User entity), I am not able to save my form correctly.

Expected results

When I save the form, only the two texts fields are maintained, the WatchTextUser I've added on the current edit only. Which means I lost every previously existing relation between WatchTextUser and User.

Actual results

I've been digging up a bit to see what's changing between both methods (displaying an existing autocomplete input and creating then selecting one), and I've discovered that the div with id ending with _hidden_inputs_wrap is supposed to hold the id of the linked entity in the value attribute.

When you make a change on the autcomplete field, the event on 'change' triggers, and append the hidden input with the id as value. But on the loading, the div _hidden_inputs_wrap is supposed to have the id set in the value attribute, but is empty instead. And the value twig variable which is supposed to contain the id data is and empty array in my case..

JeremyDulon commented 7 years ago

After some diggging, I have been able to access the id value of the User through the form.vars.data value, where I test if the data is empty and if not get the id.

If you guys have a more proper way to do so I'm listening

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.