2015-06-05 33 views
8

當我提交Symfony2的形式,我得到了以下錯誤:Symfony的2 - 實體已經進行管理或計劃刪除單計算

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

這個錯誤是什麼意思?

我正在使用旨在向DB添加新項目的表單。我在表單中有多個ManyToOne關係。

/** 
    * 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); 
     } 
+0

感謝您將代碼降低到問題分支! – Barett

回答

17

你需要堅持你的實體讓EntityManager知道它存在。

$em->persist($item); 
$em->flush($item);