2014-02-18 66 views
0

我已經編寫了symfony2中的編輯函數,我的問題是我可以從數據庫中獲取以前的值,但是在提交表單後,值創建的不是一個新窗體創建的如何在symfony2中編輯函數

這裏是控制器

 public function editAction($id){ 
     $request = $this->getRequest(); 
    $em = $this->getDoctrine()->getEntityManager(); 
    $profile= $em->getRepository('TcprofileBundle:TcProfiles') 
    ->find($id); 

     $profile_form = $this->createForm(new ProfileType(), $profile); 
     $response = new JsonResponse(); 
     $response->setData(array('content' => $this->renderView('TcprofileBundle:Default:create.html.twig',array('form' => $profile_form->createView())))); 
     return $response; 

} 

Html.twig文件

<div class="edit01"><a href="#!{{ path('tcprofile_edit',{ 'id': profile.getId}) }}"> Edit Profile </a></div> 

我取出舊的價值觀,所以我把,如果環路條件檢查這樣

控制器

public function editAction($id){ 

    $request = $this->getRequest(); 
    $em = $this->getDoctrine()->getEntityManager(); 
    $profile= $em->getRepository('TcprofileBundle:TcProfiles') 
    ->find($id); 
    if(!$id){ 
    $profile_form = $this->createForm(new ProfileType(), $profile); 
    $response = new JsonResponse(); 
    $response->setData(array('content' => $this->renderView('TcprofileBundle:Default:create.html.twig',array('form' => $profile_form->createView())))); 
    return $response; 
    }else{ 
     $profile->upload(); 
     $em->persist($profile); 
     $em->flush(); 


    $response = new JsonResponse(); 
    $response->setData(array('content' => $this->renderView('TcprofileBundle:Default:create.html.twig',array('form' => $profile_form->createView())))); 
    return $response; 
} 
} 

它得不到輸出請幫助我,如果我錯了地方

回答

0

可以更新實體時使用$em->merge($profile)代替$em->persist($profile)。請問this stack overflow

+0

如果我使用合併,我得到異常,因爲在鏈配置名稱空間Tc \ PlayerBundle \ Entity,Tc \ profileBundle \ Entity,Tc \中找不到類'Symfony \ Component \ Form \ Form' StatusBundle \ Entity,Tc \ PhotoBundle \ Entity,FOS \ UserBundle \ Entity,Tc \ UserBundle \ Entity – harshamullangi

+0

只需刪除persist()或merge()並再次嘗試它將工作。 – Nisam