Reputation: 309
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).
Upvotes: 1
Views: 2351
Reputation: 309
The problem was cache.
Restarting the server after the extra-bundle composer installation and running bin/console cache:clear
solved the issue.
Upvotes: 6