2013-08-07 37 views
1

我今天早上被困在一個實體的更新。 不知道我錯過了什麼,很確定這是一個新手的錯誤。symfony主義從形式更新

我只是想通過表單更新內容。

控制器:

public function editAction($pid, $plid, Request $request) 
{ 
    $plan = new Plan(); 
    $form = $this->createForm(new PlanType(), $plan); 

    $plan = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Plan')->findOneByPlid($plid); 
    $project = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Project')->findOneByPid($pid); 

    $form->handleRequest($request); 
    if ($request->getMethod() == 'POST') { 

      $em = $this->getDoctrine()->getManager(); 
      $em->flush(); 

      return $this->redirect($this->generateUrl('qarth_framework_plan_edit', array('pid' => $pid, 'plid' => $plid))); 
    } 

    return $this->render('QArthFrameworkBundle:Pages:plan_edit.html.twig', array(
     'plan' => $plan, 
     'project' => $project, 
     'form' => $form->createView(), 
    )); 
} 

形式:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('name'); 
    $builder->add('description', 'textarea'); 
} 

實體:http://pastebin.com/bTqKehyQ

隨着探查我可以看到我的帖子參數以及發佈

plan {"name":"fsggsfgsf","description":"gsfgsfgsf","_token":"7d089aca0203c60fe1e617488e532ac966101440"} 

但我看不到任何更新查詢或其他東西的痕跡。 如果你有一個想法,它會很棒!

非常感謝,

+1

你缺少了'堅持()'調用:'$ EM->堅持($計劃)'沖洗 – cheesemacfly

+0

之前,我與堅持()調用相同的行爲。 – benarth

回答

0

需要查詢的計劃傳遞到窗體。

public function editAction($pid, $plid, Request $request) 
{ 
    $plan = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Plan')->findOneByPlid($plid); 
    $project = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Project')->findOneByPid($pid); 

// Create a new one if not found 
if (!$plan) $plan = new Plan(); 

// Build your form using queried or new plan 
$form = $this->createForm(new PlanType(), $plan); 

$form->handleRequest($request); 

// Checks for POST as well as validity 
if ($form->isValid()) { 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($plan); // To handle new plans, no impact for existting plans 
     $em->flush(); 

// Rest is the same