我在保存冲突实体的数据时遇到了一个问题,每次调用方法时都会失败。
$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()));
}
可能很少有人关心:
如果您使用的是symfony的最新版本,即symfony 2.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);