2017-09-14 30 views
0

我在Symfony的初學者,所以我跟着一個教程回合的Symfony 3.當我嘗試刷新()我得到一個錯誤的Symfony:錯誤時的flush()

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

任何想法? 謝謝你的幫助!

回答

1

之前已沖洗

1

調用$em->persist($yourEntityToPersist);要調用flush功能BTU你不堅持任何東西到你的數據庫。

$em->persist($entity); 

因爲當flush()方法被調用,學說看起來在所有它的管理對象,看看他們是否需要被保存到數據庫。

所以你打電話flush什麼都沒有,代碼壞了。

你叫

$advert->addCategory($category); 

但此調用僅在內存中的類添加到廣告,如果你想要把這個數據到你的數據庫,你需要堅持adver這樣後再衝洗

$em->persist($advert); 
$em->flush(); 

在您保存您的類別到廣告到你的數據庫不僅在內存

0

這種情況下是不是很沒用的堅持,當它與Doctr得到實體恩?

$em = $this->getDoctrine()->getManager(); 

$advert = $em->getRepository('OCPlatformBundle:Advert')->find($id);