我是Symfony的初学者,所以我遵循了Symfony 3的教程。
"This page isn't working
localhost didn't send any data
ERR_EMPTY_RESPONSE"当我注释此行时,页面可以正常工作...代码如下:
public function editAction($id, Request $request) {
$em = $this->getDoctrine()->getManager();
$advert = $em->getRepository('OCPlatformBundle:Advert')->find($id);
if (null === $advert) {
throw new NotFoundHttpException("L'annonce d'id ".$id." n'existe pas.");
}
$listCategories = $em->getRepository('OCPlatformBundle:Category')->findAll();
foreach ($listCategories as $category) {
$advert->addCategory($category);
}
$em->flush();
if ($request->isMethod('POST')) {
$request->getSession()->getFlashBag()->add('notice', 'Annonce bien modifiée.');
return $this->redirectToRoute('oc_platform_view', array('id' => 5));
}
return $this->render('OCPlatformBundle:Advert:edit.html.twig', array(
'advert' => $advert
));
}有什么想法吗?感谢您的帮助!
发布于 2017-09-14 16:23:43
在刷新之前必须调用$em->persist($yourEntityToPersist);
发布于 2017-09-14 16:24:50
您正在调用flush函数btu,您没有将任何内容持久化到数据库中。
$em->persist($entity);因为当调用flush()方法时,Doctrine会查看它正在管理的所有对象,以确定它们是否需要持久化到数据库。
因此,您在没有调用flush的情况下调用它,并且代码被破坏了。
你打电话给
$advert->addCategory($category);但此调用仅将类别添加到广告中,如果您需要像这样持久化后将此数据放入数据库中,然后刷新
$em->persist($advert);
$em->flush();在这种情况下,您将把广告类别保存到数据库中,而不仅仅是保存在内存中
发布于 2017-09-15 16:54:28
当使用Doctrine时,持久化实体不是无用的吗?
$em = $this->getDoctrine()->getManager();
$advert = $em->getRepository('OCPlatformBundle:Advert')->find($id);https://stackoverflow.com/questions/46213990
复制相似问题