Giter Club home page Giter Club logo

Comments (6)

VincentLanglet avatar VincentLanglet commented on July 17, 2024

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.

from sonatadoctrinemongodbadminbundle.

VincentLanglet avatar VincentLanglet commented on July 17, 2024

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 ?)

from sonatadoctrinemongodbadminbundle.

franmomu avatar franmomu commented on July 17, 2024

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.

from sonatadoctrinemongodbadminbundle.

moneto4ka avatar moneto4ka commented on July 17, 2024

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.

from sonatadoctrinemongodbadminbundle.

moneto4ka avatar moneto4ka commented on July 17, 2024

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

from sonatadoctrinemongodbadminbundle.

jordisala1991 avatar jordisala1991 commented on July 17, 2024

So, this issue can be closed right?

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

from sonatadoctrinemongodbadminbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.