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

Exception is thrown when adding a new item to a collection when the owner of the collection is new as well. #6179

Closed Devristo closed 9 months ago

Devristo commented 4 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'

sonata-project/admin-bundle              3.71.1 3.71.1 The missing Symfony Admin Generator
sonata-project/block-bundle              3.20.0 4.2.0  Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/doctrine-extensions       1.6.0  1.6.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.20.0 3.20.0 Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.2.0  2.2.0  Lightweight Exporter library
sonata-project/form-extensions           1.5.0  1.5.0  Symfony form extensions
sonata-project/twig-extensions           1.3.1  1.3.1  Sonata twig extensions

Symfony packages

$ composer show --latest 'symfony/*'

symfony/asset                      v4.4.10 v4.4.10 Symfony Asset Component
symfony/browser-kit                v4.4.10 v4.4.10 Symfony BrowserKit Component
symfony/cache                      v4.4.10 v4.4.10 Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v2.1.2  v2.1.2  Generic abstractions related to caching
symfony/config                     v4.4.10 v4.4.10 Symfony Config Component
symfony/console                    v4.4.10 v4.4.10 Symfony Console Component
symfony/css-selector               v4.4.10 v4.4.10 Symfony CssSelector Component
symfony/debug                      v4.4.10 v4.4.10 Symfony Debug Component
symfony/debug-bundle               v4.4.10 v4.4.10 Symfony DebugBundle
symfony/debug-pack                 v1.0.8  v1.0.8  A debug pack for Symfony projects
symfony/dependency-injection       v4.4.10 v4.4.10 Symfony DependencyInjection Component
symfony/doctrine-bridge            v4.4.10 v4.4.10 Symfony Doctrine Bridge
symfony/dom-crawler                v4.4.10 v4.4.10 Symfony DomCrawler Component
symfony/dotenv                     v4.4.10 v4.4.10 Registers environment variables from a .env file
symfony/error-handler              v4.4.10 v4.4.10 Symfony ErrorHandler Component
symfony/event-dispatcher           v4.4.10 v4.4.10 Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.7  v2.1.2  Generic abstractions related to dispatching event
symfony/expression-language        v4.4.10 v4.4.10 Symfony ExpressionLanguage Component
symfony/filesystem                 v4.4.10 v4.4.10 Symfony Filesystem Component
symfony/finder                     v4.4.10 v4.4.10 Symfony Finder Component
symfony/flex                       v1.8.4  v1.8.4  Composer plugin for Symfony
symfony/form                       v4.4.10 v4.4.10 Symfony Form Component
symfony/framework-bundle           v4.4.10 v4.4.10 Symfony FrameworkBundle
symfony/http-foundation            v4.4.10 v4.4.10 Symfony HttpFoundation Component
symfony/http-kernel                v4.4.10 v4.4.10 Symfony HttpKernel Component
symfony/inflector                  v4.4.10 v4.4.10 Symfony Inflector Component
symfony/intl                       v4.4.10 v4.4.10 A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/maker-bundle               v1.19.0 v1.19.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.10 v4.4.10 A library to manipulate MIME messages
symfony/monolog-bridge             v4.4.10 v4.4.10 Symfony Monolog Bridge
symfony/monolog-bundle             v3.5.0  v3.5.0  Symfony MonologBundle
symfony/options-resolver           v4.4.10 v4.4.10 Symfony OptionsResolver Component
symfony/phpunit-bridge             v5.1.2  v5.1.2  Symfony PHPUnit Bridge
symfony/polyfill-intl-grapheme     v1.17.1 v1.17.1 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.17.1 v1.17.1 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.17.1 v1.17.1 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.17.1 v1.17.1 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.17.1 v1.17.1 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.17.0 v1.17.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.17.1 v1.17.1 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80             v1.17.1 v1.17.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/profiler-pack              v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access            v4.4.10 v4.4.10 Symfony PropertyAccess Component
symfony/routing                    v4.4.10 v4.4.10 Symfony Routing Component
symfony/security-acl               v3.0.4  v3.0.4  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.4.10 v4.4.10 Symfony SecurityBundle
symfony/security-core              v4.4.10 v4.4.10 Symfony Security Component - Core Library
symfony/security-csrf              v4.4.10 v4.4.10 Symfony Security Component - CSRF Library
symfony/security-guard             v4.4.10 v4.4.10 Symfony Security Component - Guard
symfony/security-http              v4.4.10 v4.4.10 Symfony Security Component - HTTP Integration
symfony/service-contracts          v2.1.2  v2.1.2  Generic abstractions related to writing services
symfony/stopwatch                  v4.4.10 v4.4.10 Symfony Stopwatch Component
symfony/string                     v5.1.2  v5.1.2  Symfony String component
symfony/templating                 v4.4.10 v4.4.10 Symfony Templating Component
symfony/test-pack                  v1.0.6  v1.0.6  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                v4.4.10 v4.4.10 Symfony Translation Component
symfony/translation-contracts      v2.1.2  v2.1.2  Generic abstractions related to translation
symfony/twig-bridge                v4.4.10 v4.4.10 Symfony Twig Bridge
symfony/twig-bundle                v4.4.10 v4.4.10 Symfony TwigBundle
symfony/validator                  v4.4.10 v4.4.10 Symfony Validator Component
symfony/var-dumper                 v4.4.10 v4.4.10 Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.4.10 v4.4.10 A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-profiler-bundle        v4.4.10 v4.4.10 Symfony WebProfilerBundle
symfony/yaml                       v4.4.10 v4.4.10 Symfony Yaml Component

PHP version

$ php -v
PHP 7.4.7 (cli) (built: Jun  9 2020 10:57:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies
    with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans

Subject

Exception is thrown when adding a new item to a collection when the owner of the collection is new as well.

Steps to reproduce

  1. Clone the reproducer at https://github.com/Devristo/reproducer-sonata-nested-collection-bug
git clone https://github.com/Devristo/reproducer-sonata-nested-collection-bug.git
cd reproducer-sonata-nested-collection-bug
composer install
bin/console doctrine:schema:update --force # Initialize sqlite db
symfony serve -d
  1. Go to https://127.0.0.1:8000/admin/app/book/create

  2. Press 'Add new' to add a new Chapter (note that clicking it a second time does NOT create a new Chapter row)

  3. Immediately (without saving the Book) press 'Add new' to add a new Page

Expected results

The expected result is a new Page for the new Chapter.

Actual results

The AJAX call fails with an error like this:

Exception:
Could not get element id from s5efc6effcc38d_chapters_0_pages Failing part: pages

  at vendor/sonata-project/admin-bundle/src/Admin/AdminHelper.php:331
  at Sonata\AdminBundle\Admin\AdminHelper->getElementAccessPath()
     (vendor/sonata-project/admin-bundle/src/Admin/AdminHelper.php:145)
  at Sonata\AdminBundle\Admin\AdminHelper->appendFormFieldElement()
     (vendor/sonata-project/admin-bundle/src/Action/AppendFormFieldElementAction.php:77)
  at Sonata\AdminBundle\Action\AppendFormFieldElementAction->__invoke()
     (vendor/symfony/http-kernel/HttpKernel.php:158)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (vendor/symfony/http-kernel/HttpKernel.php:80)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (vendor/symfony/http-kernel/Kernel.php:201)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (public/index.php:25)             

Related issues

Seems to be the same as the closed #4752 issue.

github-actions[bot] commented 3 years ago

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

daum commented 3 years ago

We just hit this bug too, can confirm.

VincentLanglet commented 3 years ago

Do you want to try to debug it @daum ?

daum commented 3 years ago

@VincentLanglet can try to look into it, but probably won't have enough time to do a deep dive until later next week, I saw a a couple of interesting issues using the debug project above as reference. If you add a second Chapter, it will let you add the "Page" in the second chapter, but still fail on the first one. It seems to keep resetting back to a single "Page" any subsequent clicks on the Second chapter. The first chapter still will error on any attempts to add a Page.

It seems like it's almost the embedded indexes of the Page's are incorrectly getting setup, but I'd need to dig into that further.

kbosilkov commented 3 years ago

The issues seems that if in the submitted form there is unchecked checkboxes. Ping @VincentLanglet @daum

https://user-images.githubusercontent.com/1915265/121401443-66e18b00-c961-11eb-906b-d48bf5fe4427.mov

VincentLanglet commented 3 years ago

It could be interesting to dump the values $elementId and $model in the getElementAccessPath, to see the differences.

kbosilkov commented 3 years ago

if ($this->propertyAccessor->isReadable($model, $totalPath.$separator.$currentPath)) this return false, but can not figured it why. When try to read the value through propertyAccessor it thrown an error Cannot read property "workTimeSlots" from an array. Maybe you intended to write the property path as "[workTimeSlots]" instead. This should be relation object, not array.

VincentLanglet commented 3 years ago

I cannot make work the reproducer... do you have one I can try ?

I would $model and other variable in order to understand more.

kbosilkov commented 3 years ago

The $model (Venue) is pretty huge object. Here are the relations:

App\Entity\Venue:
    type: entity
    table: venues
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    oneToMany:
        workTimes:
            targetEntity: App\Entity\WorkTime
            mappedBy: venue
            cascade: ["persist"]
App\Entity\WorkTime:
    type: entity
    table: working_time
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        day:
            type: smallint
            column: dow
            options:
                unsigned: true
        active:
            type: boolean
            column: is_active
            options:
                default: true
    manyToOne:
        venue:
            targetEntity: App\Entity\Venue
            inversedBy: workTimes
            joinColumn:
                name: venue_id
                referencedColumnName: id
                onDelete: CASCADE
                nullable: false
    oneToMany:
        workTimeSlots:
            targetEntity: App\Entity\WorkTimeSlot
            mappedBy: workTime
            orphanRemoval: true
            cascade: ["persist", "remove"]
            orderBy: { 'openAt': 'ASC' }
    uniqueConstraints:
        venue_dow:
            columns: [ venue_id, dow ]
App\Entity\WorkTimeSlot:
  type: entity
  table: working_time_slots
  id:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
  fields:
    openAt:
      type: time
      column: open_at
    closeAt:
      type: time
      column: close_at
  manyToOne:
    workTime:
      targetEntity: App\Entity\WorkTime
      inversedBy: workTimeSlots
      joinColumn:
        name: work_time_id
        referencedColumnName: id
        onDelete: CASCADE
        nullable: false

The form in VenueAdmin.php

$formMapper->tab('ui.tab.worktime')
      ->with('ui.box.worktime', array('class' => 'col-md-12'))
           ->add('workTimes', CollectionType::class, array(
                  'btn_add' => false,
                  'required' => false,
                  'by_reference' => false,
                  'type_options' => array(
                      'delete' => false,
                      'constraints' => [new Valid()]
                  ),
                  'label' => false
              ), array(
                  'edit' => 'inline',
                  'sortable' => 'position',
                  'inline' => 'table',
                  'admin_code' => 'admin.worktime',
              ))
      ->end()
  ->end();

WorkTimeAdmin.php

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('active', CheckboxType::class, array(
                        'label' => 'entity.venue.worktime.active',
                        'required' => false,
                    ))
                    ->add('day', ChoiceType::class, array(
                            'label' => 'entity.venue.worktime.day',
                            'multiple' => false,
                            'expanded' => false,
                            'disabled' => true,
                            'choices' => WorkTime::getDaysOfWeek(),
                            'choice_translation_domain' => 'messages',
                            'choice_label' => function ($choice, $key, $value) {
                                return sprintf("bot.dow.%s", $value);
                            }
                    ))
                    ->add('workTimeSlots', CollectionType::class, array(
                        'required' => false,
                        'type_options' => array('constraints' => [new Valid()]),
                        'label' => 'entity.venue.worktime.slots'
                    ), array(
                        'edit' => 'inline',
                        'sortable' => 'position',
                        'inline' => 'table',
                        'admin_code' => 'admin.worktime_slot',
                    ));
    }

WorkTimeSlotAdmin.php

protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('openAt', TimeType::class, array(
                'label' => 'entity.venue.openAt',
                'minutes' => array(00, 15, 30, 45)
            ))
            ->add('closeAt', TimeType::class, array(
                'label' => 'entity.venue.closeAt',
                'minutes' => array(00, 15, 30, 45)
            ));
    }

Hope this help :)

VincentLanglet commented 3 years ago

if ($this->propertyAccessor->isReadable($model, $totalPath.$separator.$currentPath)) this return false, but can not figured it why. When try to read the value through propertyAccessor it thrown an error Cannot read property "workTimeSlots" from an array. Maybe you intended to write the property path as "[workTimeSlots]" instead. This should be relation object, not array.

You said $this->propertyAccessor->isReadable($model, $totalPath.$separator.$currentPath) return false.

In order to know why, you should dump

Then, since you don't have the error when ticking the checkbox, you should dump the same values with the checkbox ticked. If there is a difference in those values it could help us to understand the issue.

I cannot debug myself without a working reproducer.

kbosilkov commented 3 years ago

Now I see that unchecked rows are not submitted at all. Then properyAccessor can't find them by their index and return false.

VincentLanglet commented 3 years ago

Empty row are not submitted.

You should try to removed disabled to the day field. Use readonly instead.

kbosilkov commented 3 years ago

Empty row are not submitted.

You should try to removed disabled to the day field. Use readonly instead.

Now works, thank you @VincentLanglet!

byhaskell commented 3 years ago

Hello. What's the solution? Can I have an example? Thanks in advance.

kbosilkov commented 3 years ago

As @VincentLanglet mentioned it, the issue was that I have empty rows in my child admin form. You can add an unmapped hidden field with some dummy data.

byhaskell commented 3 years ago

@kbosilkov Thank you. Checked, does not work. Added for the parent, then for the first child, then for the second child.

My field looked like this:

->add('fix404', HiddenType::class, array( "mapped" => false, 'empty_data' => '404', 'attr' => ['class' => 'hidden']))
byhaskell commented 3 years ago

A successful outcome can only be with a gradual approach, filling in the parent entity and the first subordinate one - saved, then added to the child - adding a second child entity. But at the last stage, even if you save, the data is not displayed either in the "table" or in "nature".