Closed antonymous closed 1 year 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.
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 ?)
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
.
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.
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
So, this issue can be closed right?
The problem was trying to create admins for things that should only be FormTypes.
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
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):
Embedded document, direct child of a root doocument (level 1 of nesting), no identifier:
Another embedded document, direct child of a previous embedded doocument (level 2 of nesting), also no identifier:
Admin classes:
services.yaml
:Steps to reproduce
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:
Now we have to override
AbstractAdmin::createNewInstance()
in each such case to omitAbstractAdmin::appendParentObject()
call, which is easy to forget.