symfony - can't update entity Symfony2 -


trying update entity, unfortunately didnt use doctrines generate crud feature (have change stuff didnt write). getting data form, won't save changes (also, doesn't create new entity 1 might suspect). when click 'save', return page have form edit entity. checked if method post, is.

 if ($form->get('save')->isclicked()) { 

doesn't seem anything, how can be?

here's rest of action:

  /**      * updates.      *      * @route("/offerweekchange/{offerid}", name="offerweekchange")      * @template("")      */     public function offerweekchangeaction(request $request, $offerid)     {         $request = $this->get('request');         if ($offerid) {             $em = $this->getdoctrine()->getmanager();             $entity = $em->getrepository('alexanderbuerkleshopbundle:promotion')->findoneby(array('id' => $offerid));              $form = $this->createform(new offerweekchangetype(), $entity);            # \doctrine\common\util\debug::dump($request->getmethod());             if ($form->get('save')->isclicked()) {                 if ($request->getmethod() == 'post') {                      $form->bind($request);                     if ($form->isvalid()) {                         $em->flush();                         return $this->redirect($this->generateurl('offerweeklist'));                         }                     }                 }              return $this->render('alexanderbuerkleshopbundle:offerweeklist:offerweekchange.html.twig',                 array('form' => $form->createview(), 'offerid' => $offerid, 'entity' => $entity));          }     } 

any appreciated.

first, here working sample of code:

public function offerweekchangeaction(request $request, $offerid)     {         if ($offerid) {             $em = $this->getdoctrine()->getmanager();             $entity = $em->getrepository('alexanderbuerkleshopbundle:promotion')->findoneby(array('id' => $offerid));              $form = $this->createform(new offerweekchangetype(), $entity);              $form->handlerequest($request);             # \doctrine\common\util\debug::dump($request->getmethod());             if ($form->isvalid()) {                $em->persist($entity);                $em->flush();                return $this->redirect($this->generateurl('offerweeklist'));             }             return $this->render('alexanderbuerkleshopbundle:offerweeklist:offerweekchange.html.twig',                 array('form' => $form->createview(), 'offerid' => $offerid, 'entity' => $entity));          }     } 

secondly, have several mistakes here, let's analyse them 1 one:

1:

if ($offerid) { 

your code nothing on else branch.

2:

$request = $this->get('request'); 

you have request parameter injected action. line redundant.

3:

$form->bind($request); 

this deprecated since 2.3. use $form->handlerequest($request) instead.

4:

$em->flush(); 

you flushing entity manager, nothing persisted, nothing happen. have persist entity first $em->persist($entity)

5:

if ($request->getmethod() == 'post') { 

the method $form->isvalid() checks also, checking post redundant.

that's it. hope helped.


Comments