简体   繁体   中英

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM