'Update Entity with a new field in a controller - Symfony 6

I'm new in Symfony, and I am trying to calculate the average of the customer reviews at the controller level.

I did a dump in the foreach shown below, where I have the entity as I want, but in the return of the postman the field I added does not exist in my object.

nb: I don't have this field in my user table

My controller:

public function __invoke(UserRepository $rep, Request $request , EntityManagerInterface $em)

{

    $user = $this->get('security.token_storage')->getToken()->getUser();

    $dataUser = $rep->findUsersData();
    $Reviews = $rep->findUsersReviews($user->getId());
    $countReviews = count($Reviews);
    $starsValues = 0;
    foreach($Reviews as $review){
        //dump($review);
        $starsValues += $review['stars'];
    }
    $reviewsuservalue = $starsValues / $countReviews;

    foreach($dataUser as $key => $userForeach){
        if($userForeach->getId() == $user->getId()){
            $userForeach->setReviewsuservalue($reviewsuservalue);
            //dump($dataUser[$key]->getReviewsuservalue());
            $em->persist($userForeach);
            $em->flush();
            //dump($em->flush());
        }
    }


    return $this->json($dataUser);

}

and I add this in my entity:

private $reviewsuservalue;

and this is my getter & setter in the mentioned entity

public function getReviewsuservalue(): ?float
{
    return $this->reviewsuservalue;
}

public function setReviewsuservalue(float $reviewsuservalue): self
{
    $this->reviewsuservalue = $reviewsuservalue;

    return $this;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source