使用persist方法在Symfony2上保存数据失败

时间:2014-07-10 00:07:04

标签: php orm doctrine-orm symfony-2.1

我在保存冲突实体数据时遇到问题,每次调用方法时都失败了。

$em->persist($conflict);

返回一个空白屏幕并显示一个字符串' persist'。因为我是symfony2的新手,所以我不知道如何解决它。

这是关于创建冲突控制器的示例代码。

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

1 个答案:

答案 0 :(得分:1)

可能很少有人担心:

如果您使用最新版本的symfony i。即(Symfony2.2或更新)然后:

$form->bindRequest($request);

应该是:

$form->handleRequest($request);

以及返回渲染线

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

当你传递一个对象来渲染对象的所有字段时,你只渲染你的id。 它可能是这样的:

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

我也不明白必须通过

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

在您的表单创建中

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

可能是这样的

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