Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 - Entity has to be managed or scheduled for removal for single computation

When I am submitting symfony2 form I got the following error:

Entity has to be managed or scheduled for removal for single computation

What does this error mean?

I am using the form which is aimed at adding new item to DB. I have multiple ManyToOne relations in the form.

   /**
    * This code is aimed at checking if the book is choseen and therefore whether any further works may be carried out
    */
    $session = new Session();
    if(!$session->get("App_Books_Chosen_Lp")) return new RedirectResponse($this->generateUrl('app_listbooks'));

    $request = $this->get('request');
    $em = $this->getDoctrine()->getManager();
    if($item_id != null)
    {
        /* THIS CODE IS NOT EXECUTED IN THE GIVEN CASE */
    }
    else 
    {
        // Add
        $item = new Items();
        $form = $this->createForm(new ItemsType(), $item);
        $form->add('save', 'submit', array('label' => 'Add item'));
    }
    $form->remove('documentid');
    $form->remove('book');
    $form->handleRequest($request);
    if ($form->isValid()) {

        if($item_id != null)
        {
            /* THIS CODE IS NOT EXECUTED IN THE GIVEN CASE */
        }
        else
        {
/* HERE ERROR OCCURS */
            // Add

            $book = $em->getReference('AppBundle:Books', $session->get("App_Books_Chosen_Lp"));
            if( $book ) $item->setBook($book);
            $doc = $em->getReference('AppBundle:Documents', $doc_id);
            if( $doc ) $item->setDocumentid($doc);
            $em->flush($item);
            $session = new session();
            $session->getFlashBag()->add('msg', 'Item was added.');
            $url = $this->generateUrl("app_documents_details", array("id" => $doc_id));
            return $this->redirect($url);
        }
like image 223
Abdel5 Avatar asked Jun 05 '15 20:06

Abdel5


1 Answers

You need to persist your entity to let the EntityManager knows that it exists.

$em->persist($item);
$em->flush($item);
like image 170
Raphaël Malié Avatar answered Sep 18 '22 08:09

Raphaël Malié