1

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:
https://symfony.com/doc/current/serializer.html

After adding the @Groups annotation as shown below:

class User implements UserInterface
{
    // ...
        /**
     * @ORM\OneToMany(targetEntity=PortfolioItem::class, mappedBy="user", orphanRemoval=true)
     * @ORM\OrderBy({"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="portfolio.brands.chart.data", 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 "App\Entity\PortfolioItem" (configured limit: 1).

1 Answer 1

6

The problem was cache. Restarting the server after the extra-bundle composer installation and running bin/console cache:clear solved the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.