SonataAdminBundle
SonataAdminBundle copied to clipboard
MongoDB's embedded documents nested twice stopped working in Sonata v.4
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
- Open Sonata Admin.
- Create new RootDoc, add one item to "Level1 Nested Docs", and one item to "Level2 Nested Docs". Save.
- 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.