Symfony Forms Error: Entity of type "…" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager

Objective

I am trying to make a basic form that signs a new player up for a sport. This is taken from the Symfony
example at:

https://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data

The Code

I have 3 entities:

PlayerList
https://github.com/ChimeraBlack1/Symphart/blob/main/src/Entity/PlayerList.php

Sport
https://github.com/ChimeraBlack1/Symphart/blob/main/src/Entity/Sport.php

Position
https://github.com/ChimeraBlack1/Symphart/blob/main/src/Entity/Position.php

I have a form:

NewPlayerType
https://github.com/ChimeraBlack1/Symphart/blob/main/src/Form/NewPlayerType.php

I have a controller:

NewPlayerController
https://github.com/ChimeraBlack1/Symphart/blob/main/src/Controller/NewPlayerController.php

The Error:

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

Details:

I seem to get this error whenever I create a form using the form builder that is of type, “EntityType” like so:

Reference:
https://github.com/ChimeraBlack1/Symphart/blob/main/src/Form/NewPlayerType.php (line 22)

    ->add('sport', EntityType::class, [
        'class' => Sport::class,
        'query_builder' => function(EntityRepository $er) {
            return $er->createQueryBuilder('s')
                ->orderBy('s.sport', 'ASC');
        },
        'choice_label' => 'sport',
    ])

It seems to me that this is happening because I am referencing "Sport::class" in the "NewPlayerType" form. If I were to reference the "PlayerList::class", I don’t experience the error. But how do I get fields based on other entities like this to populate on a single form? I’m missing something here conceptually I think…

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *