Reputation: 9
I am a beginner in Symfony so I follow a tutorial bout Symfony 3. When I try to flush() I got an error
"This page isn't working
localhost didn't send any data
ERR_EMPTY_RESPONSE"
When I comment this line, the page works... here is the code :
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
));
}
Any idea ? Thank you for your help !
Upvotes: 0
Views: 981
Reputation: 9
Isn't it useless to persist the entity when it was getting with Doctrine ?
$em = $this->getDoctrine()->getManager();
$advert = $em->getRepository('OCPlatformBundle:Advert')->find($id);
Upvotes: 0
Reputation: 35963
You are calling flush
function btu you don't persist anything into your database.
$em->persist($entity);
Because when the flush()
method is called, Doctrine looks through all of the objects that it's managing to see if they need to be persisted to the database.
So you are calling flush
on nothing and the code is broken.
You call
$advert->addCategory($category);
But this call only add in memory the category into advert, if you want to put this data into your db after you need to persist adver like this and then flush
$em->persist($advert);
$em->flush();
In this case you are saving your category into advert into your database not only in memory
Upvotes: 1