Symfony Serializer with Groups not working – empty output

I am trying to serialise data as JSON with the default Symfony Serializer.
To do that I’m trying to use @Groups() as explained here:

After adding the @Groups annotation as shown below:

class User implements UserInterface
    // ...
     * @ORMOneToMany(targetEntity=PortfolioItem::class, mappedBy="user", orphanRemoval=true)
     * @ORMOrderBy({"id" = "DESC"})
     * @Groups({"show_user"})
    private $portfolioItems;

On my controller I have the following:

     * @param Request $request
     * @return JsonResponse
     * @Route("/async/portfolio/brands/get_chart", name="", options={"expose"=true}, methods={"POST", "GET"})
     * @IsGranted("ROLE_USER")
    public function getDataForBrandsChart(Request $request): JsonResponse
        $user = $this->getUser();
        $portfolioItems = $user->getPortfolioItems();
        $output = $this->serializer->serialize($portfolioItems, "json", ["groups" => "show_user"]);

        return new JsonResponse($output, 200);

This always gives the following output:
Why is it always empty?

The reason I am using the Groups is because without them I have the following error:

A circular reference has been detected when serializing the object of class "AppEntityPortfolioItem" (configured limit: 1).

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *