Search code examples
symfonysymfony4

How to update boolean value in Symfony?


When a user comments on a post its approval is first saved as false, because the admin has to review his post and approve it. I want it to change to approved after the admin has completed their review.

This is my initial code:

if ($form->isSubmitted() && $form->isValid()){
    $data = $form->getData();
    $em = $this->getDoctrine()->getManager();

    $data->setApproval(false);
    $em->persist($data);
    $em->flush();

    // $this->redirectToRoute("view_blog");
    return $this->redirect($request->getUri());
}

After the Admin verifies user comments, he clicks the approve button, and then I do this:

$post = $this->getDoctrine()->getRepository(Comment::class)->find($id);

$data = $post->getApproval();    

$em = $this->getDoctrine()->getManager();
$data->setApproval(true);        
$em->persist($data);
$em->flush();

But, I'm getting this error:

Call to a member function setApproval() on boolean


Solution

  • Replace $data->setApproval(true); with $post->setApproval(true);

    It should be like.

    $post = $this->getDoctrine()->getRepository(Comment::class)->find($id);
    
    $em = $this->getDoctrine()->getManager();
    $post->setApproval(true);        
    $em->persist($data);
    $em->flush();