2014-07-10 25 views
0

保存衝突實體的數據時發生問題,每次調用方法時都會失敗。使用persist方法在Symfony2上保存數據失敗

$em->persist($conflict); 

返回一個空白屏幕並顯示一個字符串'persist'。我不知道如何解決它,因爲我是symfony2的新手。

這是我創建衝突控制器的示例代碼。

public function createAction() { 
    $conflict = new Conflict(); 
    $form = $this->createForm(new ConflictType(), $conflict, array(
     "container" => $this->container, 
     "em" => $this->getDoctrine()->getEntityManager() 
    )); 

    $request = $this->getRequest(); 
    $form->bindRequest($request); 
    if ($form->isValid()) { 
     $conflict->setAwardDeadlineCurrent($conflict->getAwardDeadlineInit()); 
     $em = $this->getDoctrine()->getEntityManager(); 
     $em->persist($conflict); 
     $em->flush(); 
     $request->getSession()->setFlash("notice", "Case has been created"); 
     return $this->redirect($this->generateUrl("acf_case_conflict_edit", array("id" => $conflict->getId()))); 
    } 
    return $this->render("ACFCaseBundle:Conflict:new.html.twig", array("form" => $form->createView())); 
} 
+0

你有什麼事件偵聽/事件訂閱者?這個實體「衝突」是否有任何方法「PostUpdate」「PrePersist」等?如果這是你寫的唯一的代碼,看起來沒問題(也許在「conflictType」形式上有點怪異),所以它必須是關於我們的symfony2安裝 – Cesc

+0

同意@Francesc ...聽起來像死();測試代碼的一部分是否已到達 – nixoschu

回答

1

可能有一些擔憂:

如果妳[R使用最新版本的symfony我的。即(Symfony2.2或更多最新的),那麼:

$form->bindRequest($request); 

應該是:

$form->handleRequest($request); 

,並在返回渲染行

return $this->redirect($this->generateUrl("acf_case_conflict_edit", array("id" => $conflict->getId()))); 

要渲染只是你的ID,而你應該通過一個對象來呈現對象的所有字段.. 它可能是這樣的:

return $this->redirect($this->generateUrl("acf_case_conflict_edit", array("conflict" => $conflict))); 

還我do't瞭解強制通過

array(
    "container" => $this->container, 
    "em" => $this->getDoctrine()->getEntityManager() 
) 

在你的表單創建像

$form = $this->createForm(new ConflictType(), $conflict, array(
    "container" => $this->container, 
    "em" => $this->getDoctrine()->getEntityManager() 
    )); 

這可能是這樣

$form = $this->createForm(new ConflictType(), $conflict); 
+0

謝謝我會檢查您的建議並通知您 –