Sokmesa Khiev
Sokmesa Khiev

Reputation: 2920

Saving data failed on Symfony2 with persist method

I had a problem while saving data of conflict entity and it failed every time the methods is called.

$em->persist($conflict);

return back a blank screen and show a string 'persist'. I don't know how to solve it as I am new to symfony2.

Here is my sample code on create conflict controller.

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()));
}

Upvotes: 0

Views: 279

Answers (1)

Ajeet Varma
Ajeet Varma

Reputation: 726

There may be few concern:

If u r using latest version of symfony i. e. (Symfony2.2 or more latest) then :

$form->bindRequest($request);

should be :

$form->handleRequest($request);

and also in return rendering line

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

You are rendering just your id while you should pass a object to render the all field of object .. It may be like this :

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

also i do't understand mandatory to pass the

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

in your form creation like

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

It may be like this

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

Upvotes: 1

Related Questions