2012-01-25 71 views
1

我正在使用Symfony2框架,我想用表單中的數據更新實體。使用表單值更新實體Symfony2框架

我使用相同的控制器來填充窗體的一些數據,同時我正在使用它來在數據庫中進行更新查詢。如果我使用$ em-> persist($ foo),它確實保存了我想要的內容,但我不想保存它,因爲它是新數據,我想更新。

閱讀symfony2的書,它說$ em-> flush()是我們需要的,所以我們可以更新。

我想我真的很接近,但我當然錯過了一些東西。

下面的代碼:

public function actualizarCurriculoAction($id){ 

    $curriculo = new Curriculittle(); 
    $form = $this->createForm(new CurriculittleType(), $curriculo); 
    $request = $this->getRequest(); 

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

    $form->bindRequest($request); 

    $lselect=$this->get('request')->request->get('lselect'); 
    $edad=$this->get('request')->request->get('edad'); 
    $estado=$this->get('request')->request->get('estadoselect'); 

    if ($form->isValid()) { 


     $curriculo->setUsalentes($lselect); 
     $curriculo->setEdad($edad); 
     $curriculo->setEstado($estado);  

     $em = $this->getDoctrine()->getEntityManager(); 
     /*em->persist($curriculo);*/ 

     $em->flush(); /*the above line is in comment because I just want to update*/ 

          /*At this point the entity should be updated, but it's not*/ 

     /*Llamando a la plantilla de listados*/ 
     $curriculos = $em->getRepository('SofLaSoflaBundle:Curriculittle')->findAll(); 

     /*Enviando los datos a la plantilla y Renderizandola*/ 
     return $this->render('SofLaSoflaBundle:Default:listado.html.twig', array('curriculos' => $curriculos)); 
    } 
    } 

    $em = $this->getDoctrine()->getEntityManager(); 
    $trabajador=$em->getRepository('SofLaSoflaBundle:Curriculittle')->find($id); 
    return $this->render('SofLaSoflaBundle:Default:curriculo.html.twig', array('form' => $form->createView(), 'curriculo' => $trabajador)); 
} 

因此,需要幫助這個請。 :)

回答

5

在你的榜樣,你要創建一個新的實體入手:

$curriculo = new Curriculittle(); 

因爲這不是一個預先存在的實體(例如,一個您通過數據庫檢索查詢),調用$em->persist($curriculo),然後按$em->flush()將該實體保存爲新項目。由於實體是新的,所以更新沒有任何內容。

持久化和刷新的相同代碼可用於更新現有實體;您只需要首先檢索/獲取現有實體,而不是創建一個新實體。目前,它看起來像你這樣做是對的方法結束:

$trabajador=$em->getRepository('SofLaSoflaBundle:Curriculittle')->find($id); 

但是你應該結合你的形式,例如之前做到這一點:

public function actualizarCurriculoAction($id) { 

    $em = $this->getDoctrine()->getEntityManager(); 
    $curriculo = $em->getRepository('SofLaSoflaBundle:Curriculittle')->find($id); 
    if (!$curriculo) { 
    $curriculo = new Curriculittle(); 
    } 
    $form = $this->createForm(new CurriculittleType(), $curriculo); 
    $request = $this->getRequest(); 

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

    $lselect=$this->get('request')->request->get('lselect'); 
    $edad=$this->get('request')->request->get('edad'); 
    $estado=$this->get('request')->request->get('estadoselect'); 

    if ($form->isValid()) { 
     $curriculo->setUsalentes($lselect); 
     $curriculo->setEdad($edad); 
     $curriculo->setEstado($estado);  

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

     /*Llamando a la plantilla de listados*/ 
     $curriculos = $em->getRepository('SofLaSoflaBundle:Curriculittle')->findAll(); 

     /*Enviando los datos a la plantilla y Renderizandola*/ 
     return $this->render('SofLaSoflaBundle:Default:listado.html.twig', array('curriculos' => $curriculos)); 
    } 
    } 

    return $this->render('SofLaSoflaBundle:Default:curriculo.html.twig', array('form' => $form->createView(), 'curriculo' => $curriculo)); 
} 

這個例子(調整以適應)將查找實體,然後將表單的提交綁定到該實體。如果實體存在,它將被更新。如果不是,它將被保存爲一個新的實體。

+0

嗨richsage!非常感謝。它確實工作:)。 –

+0

@FranciscoOchoa的樂趣:-) – richsage

+1

由於Symfony2.3,你應該使用'handleRequest'而不是'bindRequest' http://symfony.com/doc/current/book/forms.html#handling-form-submissions –