Martijn van Sliedregt
Martijn van Sliedregt

Reputation: 31

Symfony - I get circular reference error in index of controller

Hi I'm having troubles getting a list of all "blog tag" entities in the index function of my controller.

I tried using normalizer groups but somehow I still get a circular reference error. I expect my controller to output a list of blog tags, by id and name.

This is my controller:

    #[Route('/', name: 'api_blogtag_index', methods: ['GET'])]
    #[IsGranted('IS_AUTHENTICATED')]
    public function index(BlogtagRepository $blogtagRepository): Response
    {
        return $this->json([
            'tags' => $blogtagRepository->findAll(),
            Response::HTTP_OK, [], [
                AbstractNormalizer::GROUPS => ['show_blogtag']
            ]
        ]);
    }

And this is the blog tag entity class:

#[ORM\Entity(repositoryClass: BlogtagRepository::class)]
class Blogtag
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    #[Groups(['show_blogtag'])]
    private ?int $id = null;

    #[ORM\Column(length: 255, unique: true)]
    #[Groups(['show_blogtag'])]
    private ?string $name = null;

    #[ORM\ManyToMany(targetEntity: Blog::class, inversedBy: 'blogtags')]
    private Collection $blogs;

Upvotes: 0

Views: 70

Answers (1)

Yolserve Ngoma
Yolserve Ngoma

Reputation: 1

Circular reference is an error that appears when an object refers to itself, directly. This problem has already been solved, here you will find an answer of your problem. You can learn more on official documentation

Upvotes: 0

Related Questions