sonata-project / SonataDoctrineMongoDBAdminBundle

Symfony Sonata / Integrate Doctrine MongoDB ODM into the SonataAdminBundle
https://docs.sonata-project.org/projects/SonataDoctrineMongoDBAdminBundle
MIT License
65 stars 95 forks source link

MongoDB's embedded documents nested twice stopped working in Sonata v.4 #789

Closed antonymous closed 1 year ago

antonymous commented 2 years ago

Environment

Sonata packages

show

``` $ composer show --latest 'sonata-project/*' Info from https://repo.packagist.org: #StandWithUkraine Color legend: - patch or minor release available - update recommended - major release available - update possible - up to date version sonata-project/admin-bundle 4.18.0 4.18.0 The missing Symfony Admin Generator sonata-project/block-bundle 4.17.0 4.17.0 Symfony SonataBlockBundle sonata-project/cache 2.2.0 2.2.0 Cache library Package sonata-project/cache is abandoned, you should avoid using it. No replacement was suggested. sonata-project/doctrine-extensions 2.0.1 2.0.1 Doctrine2 behavioral extensions sonata-project/doctrine-mongodb-admin-bundle 4.6.0 4.6.0 Symfony Sonata / Integrate Doctrine MongoDB ODM into the SonataAdminBundle sonata-project/exporter 3.0.0 3.0.0 Lightweight Exporter library sonata-project/form-extensions 1.18.0 1.18.0 Symfony form extensions sonata-project/twig-extensions 2.0.0 2.0.0 Sonata twig extensions ```

Symfony packages

show

``` $ composer show --latest 'symfony/*' Color legend: - patch or minor release available - update recommended - major release available - update possible - up to date version symfony/asset v5.4.7 v6.1.0 Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files symfony/cache v5.4.11 v6.1.3 Provides an extended PSR-6, PSR-16 (and tags) implementation symfony/cache-contracts v2.5.2 v3.1.1 Generic abstractions related to caching symfony/config v5.4.11 v6.1.3 Helps you find, load, combine, autofill and validate configuration values of any kind symfony/console v5.4.12 v6.1.4 Eases the creation of beautiful and testable command line interfaces symfony/dependency-injection v5.4.11 v6.1.3 Allows you to standardize and centralize the way objects are constructed in your application symfony/deprecation-contracts v3.1.1 v3.1.1 A generic function and convention to trigger deprecation notices symfony/doctrine-bridge v5.4.11 v6.1.3 Provides integration for Doctrine with various Symfony components symfony/dotenv v5.4.5 v6.1.0 Registers environment variables from a .env file symfony/error-handler v5.4.11 v6.1.3 Provides tools to manage errors and ease debugging PHP code symfony/event-dispatcher v5.4.9 v6.1.0 Provides tools that allow your application components to communicate with each other by dispatching events and listening to them symfony/event-dispatcher-contracts v3.1.1 v3.1.1 Generic abstractions related to dispatching event symfony/expression-language v5.4.11 v6.1.3 Provides an engine that can compile and evaluate expressions symfony/filesystem v5.4.12 v6.1.4 Provides basic utilities for the filesystem symfony/finder v5.4.11 v6.1.3 Finds files and directories via an intuitive fluent interface symfony/flex v2.2.3 v2.2.3 Composer plugin for Symfony symfony/form v5.4.12 v6.1.4 Allows to easily create, process and reuse HTML forms symfony/framework-bundle v5.4.12 v6.1.4 Provides a tight integration between Symfony components and the Symfony full-stack framework symfony/http-foundation v5.4.12 v6.1.4 Defines an object-oriented layer for the HTTP specification symfony/http-kernel v5.4.12 v6.1.4 Provides a structured process for converting a Request into a Response symfony/options-resolver v5.4.11 v6.1.0 Provides an improved replacement for the array_replace PHP function symfony/password-hasher v5.4.11 v6.1.3 Provides password hashing utilities 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-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-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/property-access v5.4.11 v6.1.3 Provides functions to read and write from/to an object or array using a simple string notation symfony/property-info v5.4.11 v6.1.3 Extracts information about PHP class' properties using metadata of popular sources symfony/routing v5.4.11 v6.1.3 Maps an HTTP request to a set of configuration variables symfony/runtime v5.4.11 v6.1.3 Enables decoupling PHP applications from global state symfony/security-acl v3.3.1 v3.3.1 Symfony Security Component - ACL (Access Control List) symfony/security-bundle v5.4.11 v6.1.3 Provides a tight integration of the Security component into the Symfony full-stack framework symfony/security-core v5.4.11 v6.1.4 Symfony Security Component - Core Library symfony/security-csrf v5.4.11 v6.1.0 Symfony Security Component - CSRF Library symfony/security-guard v5.4.9 v5.4.9 Symfony Security Component - Guard symfony/security-http v5.4.12 v6.1.4 Symfony Security Component - HTTP Integration symfony/service-contracts v2.5.2 v3.1.1 Generic abstractions related to writing services symfony/string v5.4.12 v6.1.4 Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way symfony/translation v5.4.12 v6.1.4 Provides tools to internationalize your application symfony/translation-contracts v2.5.2 v3.1.1 Generic abstractions related to translation symfony/twig-bridge v5.4.12 v6.1.4 Provides integration for Twig with various Symfony components symfony/twig-bundle v5.4.8 v6.1.1 Provides a tight integration of Twig into the Symfony full-stack framework symfony/validator v5.4.12 v6.1.4 Provides tools to validate values symfony/var-dumper v5.4.11 v6.1.3 Provides mechanisms for walking through any arbitrary PHP variable symfony/var-exporter v5.4.10 v6.1.3 Allows exporting any serializable PHP data structure to plain PHP code symfony/yaml v5.4.12 v6.1.4 Loads and dumps YAML files ```

PHP version

$ php -v
PHP 8.1.9 (cli) (built: Aug 15 2022 09:39:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.9, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.9, Copyright (c), by Zend Technologies
    with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans

Subject

We use MongoDB and its embedded documents. Sometimes with nest them, twice. It worked in Sonata v.3. But since this changes it does not work anymore, because an embedded document has no identity, but for some reason it is now required.

Minimal repository with the bug

Root document with an identifier (i.e. entity):

<?php

namespace App\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document
 */
class RootDoc
{
    /**
     * @ODM\Id
     */
    private $id;

    /**
     * @ODM\Field
     */
    private $name;

    /**
     * @ODM\EmbedMany(targetDocument="App\Document\Level1NestedDoc")
     */
    private $level1NestedDocs;

    public function __construct()
    {
        $this->level1NestedDocs = new ArrayCollection();
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getLevel1NestedDocs()
    {
        return $this->level1NestedDocs;
    }

    public function setLevel1NestedDocs($level1NestedDocs)
    {
        foreach ($level1NestedDocs as $item) {
            $this->addLevel1NestedDoc($item);
        }
    }

    public function addLevel1NestedDoc(Level1NestedDoc $level1NestedDoc)
    {
        if (!$this->level1NestedDocs->contains($level1NestedDoc)) {
            $this->level1NestedDocs->add($level1NestedDoc);
        }
    }
}

Embedded document, direct child of a root doocument (level 1 of nesting), no identifier:

<?php

namespace App\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\EmbeddedDocument
 */
class Level1NestedDoc
{
    /**
     * @ODM\Field
     */
    private $name;

    /**
     * @ODM\EmbedMany(targetDocument="App\Document\Level2NestedDoc")
     */
    private $level2NestedDocs;

    public function __construct()
    {
         $this->level2NestedDocs = new ArrayCollection();
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getLevel2NestedDocs()
    {
        return $this->level2NestedDocs;
    }

    public function setLevel2NestedDocs($level2NestedDocs)
    {
        foreach ($level2NestedDocs as $item) {
            $this->addLevel2NestedDoc($item);
        }
    }

    public function addLevel2NestedDoc(Level2NestedDoc $level2NestedDoc)
    {
        if (!$this->level2NestedDocs->contains($level2NestedDoc)) {
            $this->level2NestedDocs->add($level2NestedDoc);
        }
    }
}

Another embedded document, direct child of a previous embedded doocument (level 2 of nesting), also no identifier:

<?php

namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\EmbeddedDocument
 */
class Level2NestedDoc
{
    /**
     * @ODM\Field
     */
    private $name;

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

Admin classes:

<?php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\Form\Type\CollectionType;

class RootDocAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        $form->add('name');
        $form->add('level1NestedDocs', CollectionType::class, [], [
            'edit' => 'inline',
            'inline' => 'table',
        ]);
    }

    protected function configureListFields(ListMapper $list): void
    {
        $list->addIdentifier('name', null, [
            'route' => ['name' => 'edit'],
        ]);
    }
}
<?php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\Form\Type\CollectionType;

class Level1NestedDocAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        $form->add('name');
        $form->add('level2NestedDocs', CollectionType::class, [], [
            'edit' => 'inline',
            'inline' => 'table',
        ]);
    }
}
<?php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;

class Level2NestedDocAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        $form->add('name');
    }
}

services.yaml:

services:
    admin.root_doc:
        class: App\Admin\RootDocAdmin
        tags:
            -
                name: sonata.admin
                model_class: App\Document\RootDoc
                manager_type: doctrine_mongodb
                label: RootDoc

    admin.level1_nested_doc:
        class: App\Admin\Level1NestedDocAdmin
        tags:
            -
                name: sonata.admin
                model_class: App\Document\Level1NestedDoc
                manager_type: doctrine_mongodb
                label: Level1NestedDoc
                show_in_dashboard: false

    admin.level2_nested_doc:
        class: App\Admin\Level2NestedDocAdmin
        tags:
            -
                name: sonata.admin
                model_class: App\Document\Level2NestedDoc
                manager_type: doctrine_mongodb
                label: Level2NestedDoc
                show_in_dashboard: false

Steps to reproduce

  1. Open Sonata Admin.
  2. Create new RootDoc, add one item to "Level1 Nested Docs", and one item to "Level2 Nested Docs". Save.
  3. Go edit this RootDoc and add another item to "Level2 Nested Docs". Press "Save".

Expected results

RootDoc saved, it contains 1 "Level1 Nested Docs" item and 2 "Level2 Nested Docs" items.

Actual results

InvalidArgumentException with message "Class "App\Document\Level1NestedDoc" does not have an identifier" is thrown.

Stack trace:

InvalidArgumentException:
Class "App\Document\Level1NestedDoc" does not have an identifier

  at vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php:1668
  at Doctrine\ODM\MongoDB\UnitOfWork->tryGetById()
     (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php:127)
  at Doctrine\ODM\MongoDB\Repository\DocumentRepository->find()
     (vendor/sonata-project/doctrine-mongodb-admin-bundle/src/Model/ModelManager.php:80)
  at Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager->find()
     (vendor/sonata-project/admin-bundle/src/Admin/AbstractAdmin.php:856)
  at Sonata\AdminBundle\Admin\AbstractAdmin->getObject()
     (vendor/sonata-project/admin-bundle/src/Admin/AbstractAdmin.php:2292)
  at Sonata\AdminBundle\Admin\AbstractAdmin->appendParentObject()
     (vendor/sonata-project/admin-bundle/src/Admin/AbstractAdmin.php:1893)
  at Sonata\AdminBundle\Admin\AbstractAdmin->createNewInstance()
     (vendor/sonata-project/admin-bundle/src/Admin/AbstractAdmin.php:781)
  at Sonata\AdminBundle\Admin\AbstractAdmin->getNewInstance()
     (vendor/sonata-project/admin-bundle/src/Form/Type/AdminType.php:89)
  at Sonata\AdminBundle\Form\Type\AdminType->buildForm()
     (vendor/symfony/form/ResolvedFormType.php:131)
  at Symfony\Component\Form\ResolvedFormType->buildForm()
     (vendor/symfony/form/FormFactory.php:73)
  at Symfony\Component\Form\FormFactory->createNamedBuilder()
     (vendor/symfony/form/FormFactory.php:39)
  at Symfony\Component\Form\FormFactory->createNamed()
     (vendor/symfony/form/Form.php:884)
  at Symfony\Component\Form\Form->add()
     (vendor/sonata-project/form-extensions/src/EventListener/ResizeFormListener.php:153)
  at Sonata\Form\EventListener\ResizeFormListener->preSubmit()
     (vendor/symfony/event-dispatcher/EventDispatcher.php:230)
  at Symfony\Component\EventDispatcher\EventDispatcher->callListeners()
     (vendor/symfony/event-dispatcher/EventDispatcher.php:59)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch()
     (vendor/symfony/event-dispatcher/ImmutableEventDispatcher.php:33)
  at Symfony\Component\EventDispatcher\ImmutableEventDispatcher->dispatch()
     (vendor/symfony/form/Form.php:568)
  at Symfony\Component\Form\Form->submit()
     (vendor/symfony/form/Form.php:589)
  at Symfony\Component\Form\Form->submit()
     (vendor/symfony/form/Form.php:589)
  at Symfony\Component\Form\Form->submit()
     (vendor/symfony/form/Form.php:589)
  at Symfony\Component\Form\Form->submit()
     (vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php:109)
  at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest()
     (vendor/symfony/form/Form.php:503)
  at Symfony\Component\Form\Form->handleRequest()
     (vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php:300)
  at Sonata\AdminBundle\Controller\CRUDController->editAction()
     (vendor/symfony/http-kernel/HttpKernel.php:153)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (vendor/symfony/http-kernel/HttpKernel.php:75)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (vendor/symfony/http-kernel/Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (vendor/autoload_runtime.php:35)
  at require_once('/var/www/testcase/vendor/autoload_runtime.php')
     (public/index.php:5)                

Now we have to override AbstractAdmin::createNewInstance() in each such case to omit AbstractAdmin::appendParentObject() call, which is easy to forget.

VincentLanglet commented 2 years ago

I don't think this was supposed to be supported previously because an admin is meant to be used for a persisted entity with an identifier. In your example you can't access to the list, the show or the edit page of the Level1NestedDocAdmin or the Level2NestedDocAdmin. You only use them for you collectionType. (You're basically creating an whole admin just to have a formType when you could create your own formType.)

Looking at the stack trace, the getObject of the Admin is not working, and that's the consequence of the fact you're using the admin for an entity without identifier. This rely on the modelManager

And if you look at the method of the modelManager https://github.com/sonata-project/SonataDoctrineMongoDBAdminBundle/blob/4.x/src/Model/ModelManager.php almost all the method won't work for your use case. You don't persist/flush an embedded and there is no sens to use find, getIdentifiersValue, ...

If something should/will be done for this "feature/fix" it should be done there IMHO. (Like a try catch of some isEmbedded checks if the documentManager provides some) ; but I'm not sure we should. @jordisala1991 WDYT ?

Another solution for you would be to implements a FakeModelManager doing nothing and using it for Embedded classes.

VincentLanglet commented 2 years ago

I transfer the issue to SonataDoctrineMongoDBAdminBundle since I believe the check should be done here. Maybe @franmomu can help.

@antonymous The getObject method is doing

final public function getObject($id): ?object
    {
        if (null === $id) {
            return null;
        }

        $object = $this->getModelManager()->find($this->getClass(), $id);
        if (null === $object) {
            return null;
        }

If the embedded has no identifier, was is the value passed to this method ? If it's null, it would have been handled by the early null check.

Unless we have a specific value, the fix will certainly be here https://github.com/sonata-project/SonataDoctrineMongoDBAdminBundle/blob/4.x/src/Model/ModelManager.php#L76-L81, with a different behavior for Embedded class (always returning null ?)

franmomu commented 2 years ago

One way to workaround this I guess is to not create a Level2NestedDocAdmin and create a Level2NestedDocType with name and in Level1NestedDocAdmin add a CollectionType from Symfony with Level2NestedDocType as entry_type.

moneto4ka commented 1 year ago

I have common situation. I have Category Document, it has "brands" embed collection of CategoryBrand documents, CategoryBrand document has "series" embed collection of BrandSeries documents. Here are my documents:

#[MongoDB\Document(repositoryClass: CategoryRepository::class)]

 class Category {

    #[MongoDB\Id(type:Type::STRING, strategy:"NONE")]
    private string $id;

    #[MongoDB\Field(type: Type::STRING)]
    private string|null $name;

    #[MongoDB\EmbedMany(targetDocument: CategoryBrand::class)]
    private $brands;

   ......
}

#[MongoDB\EmbeddedDocument]
class CategoryBrand
{
    #[MongoDB\Id(type:Type::STRING, strategy:"NONE")]
    private $id;

    #[MongoDB\Field(type: Type::STRING)]
    private $name;

    #[MongoDB\EmbedMany(targetDocument: BrandSeries::class)]
    private $series;
  .........
}

#[MongoDB\EmbeddedDocument]
class BrandSeries
{
    #[MongoDB\Id(type:Type::STRING, strategy:"NONE")]
    private $id;

    #[MongoDB\Field(type: Type::STRING)]
    private $name;

    #[MongoDB\Field(type: Type::STRING)]
    private $description;
}

So I created CategoryBrandType, BrandSeriesType. I added BrandSeriesType as CollectionType in CategoryBrandType

public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
...
            ->add('series', CollectionType::class, ['entry_type' => BrandSeriesType::class])
...
    }

I have CategoryAdmin

protected function configureFormFields(FormMapper $form): void
{

        $form
            ->tab('Category')
                ->with('Common')
                    ->add('id', null, array('label' => 'ID'))
                    ->add('name', null, array('label' => 'Name'))
                    ->add('order', null, array('label' => 'Order'))
                    ->add('svgIcon', TextareaType::class, array('label' => 'svgIcon', 'required' => false))
                ->end()
..........
            ->end()
        ;

        $form->tab('Brands', ['class' => 'col-md-8'])
            ->add('brands', CollectionType::class, [
                    'entry_type' => CategoryBrandType::class,
                    'allow_add' => true,
                    'allow_delete' => true
                ]
            )
            ->end()
        ->end()
        ;
    }

And until now all works just fine, I have an edit form, I can create/edit/delete Category data, I can create/edit/delete all CategoryBrand (in it's own tab) for category, I can even edit BrandSeries data for each CategoryBrand. But I don't get how to append create/delete functionality for that 2nd lvl embed collection (BrandSeries). And it would be just perfect if it was possible adding tabs into each CategoryBrand form.

moneto4ka commented 1 year ago

I found the solution!

public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
...
            ->add('series', CollectionType::class, [
                    'entry_type' => BrandSeriesType::class,
                    'allow_add' => true,
                    'allow_delete' => true
            ])
...
...
    }

CollectionType should be not Symfony but Sonata\AdminBundle\Form\Type\CollectionType

jordisala1991 commented 1 year ago

So, this issue can be closed right?

The problem was trying to create admins for things that should only be FormTypes.