EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.04k stars 1.02k forks source link

Collection can't be edited #5876

Open Asyjjan opened 1 year ago

Asyjjan commented 1 year ago

Hi, i'm on symfony 6.3 and easyadmin 4.7.2 and i got an entity order that has products in ManyToMany, which has toppings in ManyToMany too.

I created in the order crud a collectionField for products which take a FormType that I have created to have a collection of products and for each product you can add one or more toppings, it works great at the created but when I want to edit the form brings me an error that I can't solve and understand.

My field

CollectionField::new('products')
                ->setLabel("Produits")
                ->setEntryType(ProductFormType::class)
                ->hideOnIndex(),

My formtype

class ProductFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', EntityType::class, [
                'class' => Product::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('p');
                },
                'choice_label' => 'name', 
                'label' => 'Produit', 
            ])
            ->add('supplements', EntityType::class, [
                'class' => Supplement::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('p');
                },
                'multiple' => true,
                'expanded' => true,
                'choice_label' => 'name', // Remplacez par le nom de l'attribut que vous voulez afficher
                'label' => 'Suppléments', // Libellé du champ
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Product::class,
        ]);
    }
}

the error i'm getting

Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue(): Argument #1 ($object) must be of type ?object, string given, called in /var/www/CaisseAtixys/vendor/symfony/form/ChoiceList/Loader/AbstractChoiceLoader.php on line 49

When I tried to debug AbstractChoiceLoader on line 49, I got my list of toppings (objects) and at the ends I have this

IdReader.php on line 79:
null
AbstractChoiceLoader.php on line 49:
array:1 [▼
  0 => ""
] 
Asyjjan commented 1 year ago

So I tried to do it without easyadmin and I got the same error, I think it's more about the formType but I can't guess why

toci-to-ci commented 1 year ago

the problem is that FOR SIMPLE TASKS as get list of values from database into form you have to write a lot of unnesesery code. It is called Data Transformers.

https://ourcodeworld.com/articles/read/1388/how-to-solve-symfony-5-exception-argument-1-passed-to-symfonybridgedoctrineformchoicelistidreader-getidvalue-must-be-an-object-or-null-string-given