B
B
BonBon Slick2018-09-19 12:05:59
symfony
BonBon Slick, 2018-09-19 12:05:59

A circular reference has been detected?

A circular reference has been detected when serializing the object of class "Proxies\__CG__\App\Entity\Province" (configured limit: 1)

dump(json_encode($slidingPagination));
        dump($slidingPagination);

...
"{}"

SlidingPagination {#2339 ▼
  -route: "search"
  -params: array:2 [▼
    "location" => "heel-nederland"
    "params" => ""
  ]
  -pageRange: 5
  -template: "includes/partials/pagination.html.twig"
  -sortableTemplate: "@KnpPaginator/Pagination/sortable_link.html.twig"
  -filtrationTemplate: "@KnpPaginator/Pagination/filtration.html.twig"
  #currentPageNumber: 1
  #numItemsPerPage: 15
  #items: array:15 [▼
    0 => User {#2549 ▶}
    1 => User {#2883 ▶}
    2 => User {#2903 ▶}
...
  ]
  #totalCount: 1255
  #paginatorOptions: array:6 [▼
    "pageParameterName" => "page"
    "sortFieldParameterName" => "sort"
    "sortDirectionParameterName" => "direction"
    "filterFieldParameterName" => "filterField"
    "filterValueParameterName" => "filterValue"
    "distinct" => true
  ]
  #customParameters: array:1 [▼
    "sorted" => true
  ]
}

Error throws serializer
dump($this->serializer->serialize($slidingPagination, 'json', ['groups' => 'user']));

Groups are configured only for the user
Relationships are just like this
/**
     * @ORM\ManyToOne(targetEntity="Province", inversedBy="municipalities", cascade={"persist"})
     * @ORM\JoinColumn(name="provinceId", referencedColumnName="id", nullable=false)
     */
    private $province;

...

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Municipality", mappedBy="province")
     */
    private $municipalities;

If you do it according to the dock https://symfony.com/doc/current/components/seriali...
without injection
$encoder = new JsonEncoder();
        $normalizer = new ObjectNormalizer();
        $normalizer->setCircularReferenceLimit(1);

        $normalizer->setCircularReferenceHandler(function ($object) {
            if(method_exists($object, 'getId')){
                return $object->getId();
            }
        });

        $serializer = new Serializer(array($normalizer), array($encoder));
        dump($serializer->serialize($slidingPagination->getItems(), 'json', ['groups' => 'user']));


Maximum function nesting level of '256' reached, aborting!

The user has only 2-3 fields open
/**
     * @var string
     *
     * @ORM\Column(name="slug", type="string", length=255, unique=true)
     *
     * @Groups({"user"})
     */
    private $slug;

And that's it, nothing else. I haven't put any groups on the resolutions yet.
Please tell me how to solve?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fishernet, 2018-10-05
@BonBonSlick

I did this to forget about the circular reference once and for all:

// config/packages/framework.yaml
framework:
    serializer:
        circular_reference_handler: App\Utils\CircularReferenceHandler

// src/Utils/CircularReferenceHandler.php
<?php

namespace App\Utils;

class CircularReferenceHandler
{
    public function __invoke($object)
    {
        return $object->getId();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question