Reputation: 3723
Well.. here is the Method:
public function likeUnlikeAction($category, $id, $type)
{
$status = false;
$message = '';
$em = $this->getDoctrine()->getManager();
$article = $em->getRepository("AppBundle:Article")->find($id);
if ($type == 'like') {
$article->setLikes($article->getLikes() + 1);
} else {
$article->setLikes($article->getLikes() - 1);
}
$em->persist($article);
$em->flush();
$likes = $article->getLikes();
$response = array(
'status' => $status,
'message' => $message,
'likes' => $likes
);
return new JsonResponse($response);
}
How can I check if everything is fine and update $message
and $status
When $em->persist()
and $em->flush()
returns always null ?
Upvotes: 0
Views: 267
Reputation: 11
If you can get your article and if your getters/setters works you don't have to worrie about $em->persist()
and $em->flush()
You can set your message and status when you don't find the article. Also you can compare the 'likes' number before and after update to see if the update have been applied.
From symfony docs : Notice that calling $em->persist($product) isn't necessary. Recall that this method simply tells Doctrine to manage or "watch" the $product object. In this case, since you fetched the $product object from Doctrine, it's already managed.
Upvotes: 1