'Error : The entity passed to the choice field must be managed

In a search form, when I submit for the second time the datas containing a equipment criterion, I receive this error :

Entity of type "App\Entity\Equipment" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?

There is a example flow to reproduce the problem :

  1. I introduce criteria (without equipment criterion) to find the corresponding adverts
  2. I submit the serach form
  3. The adverts are showed without problem
  4. I introduce one or many equipment criteria (via a multiple choices list of entity type)
  5. I submit the serach form
  6. The adverts are showed without problem
  7. I introduce for the second time one or many new equipment criteria
  8. I submit the search form
  9. I receive the error :

Entity of type "App\Entity\Equipment" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?

Somebody have an idea to resolve this problem?

Thanks for your help.



Solution 1:[1]

In your CrudController, have you

*AssociationField::new('criterion')
                ->setLabel('AS_U_Want')
                ->setFormType(ChoiceType::class)
                ->setFormattedValue('name')
                ->setFormTypeOptions([
                    'by_reference' => true,      // !! Important
                ])
                ->autocomplete(),*

??

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VDevS