Marc Torramilans
Marc Torramilans

Reputation: 1

Symfony2 Exception: Entities passed to the choice field must be managed. Maybe persist them in the entity manager?

I have a FormType without entity, used to filter elements of a collection, where one of the form fields are EntityType. When the fields are choosed to filter it and submit the form, these are stored in an session array, and when come back to the index view I create a query with the session stored fields.

The problem is when the form is submited with an element choosed in Entity field, symfony throws the exception "Entities passed to the choice field must be managed". This works fine with the other fields.

I go through the Doctrine code and the error is thrown in the class IdReader when check if the object is inside the ObjectManager:

if (!$this->om->contains($object)) {
    throw new RuntimeException(
        'Entities passed to the choice field must be managed. Maybe '.
        'persist them in the entity manager?'
    );
}

I search for a solution but none of them works for me. I have these kind of filters in other projects and works fine. I think it's a kind of configuration issue that does not allow to manage the entities correctly, but I have 'auto_mapping: true' in doctrine configuration.

This is my FormType:

public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder
        ->add('estat','choice', array(
            'required' => false,
            'choices' => Relacio::$arrayEstat,
            'multiple' => true,
            'placeholder' => '--',
            'label' => 'Estat'
        ))
        ->add('institucio', 'entity', [
            'label' => 'Institució',
            'class' => 'RelacioBundle:Institucio',
            'required' => false,
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('s')
                        ->addOrderBy('s.id', 'ASC');
            }
        ])
    ;
}

This is my Controller function to apply the filter:

public function applyFilterAction(Request $request) {
    $filtre = [];
    $sessio = $this->get('session');

    $form = $this->createForm(
        new RelacioFiltreType(),
        $filtre,
        [
            'action' => $this->generateUrl('relacio_aplicar_filtre'),
            'method' => 'POST'
        ]
    );

    $form->handleRequest($request);

    if ($form->isValid()) {
        $dades = $form->getData();
        $sessio->set('relacio.filtre.institucio',$dades['institucio'] ? $dades['institucio'] : null);
        return $this->redirectToRoute('relacions',['request' => $request]);
    }

    return array(
        'form' => $form->createView()
    );
}

Thanks in advance.

Upvotes: 0

Views: 2150

Answers (2)

Marc Torramilans
Marc Torramilans

Reputation: 1

I found a solution. I change the data passed to the session about the entity, using its id instead of the whole entity.

I set the session value like this:

$sessio->set('relacio.filtre.institucio',$dades['institucio'] ? $dades['institucio']->getId() : null);

And I got the value like this:

$dades_filtre['institucio'] = $sessio->get('relacio.filtre.institucio') ? $em->getRepository('XalocSafErpBundle:SpTrbInstit')->find($sessio->get('relacio.filtre.institucio')) : null;

Thanks at all.

Upvotes: 0

Houssem Balty
Houssem Balty

Reputation: 33

If you have a constructor in your entity try removing it. I had the same problem and that solved it.

Also see : Response for the same question

Upvotes: 0

Related Questions