Answer the question
In order to leave comments, you need to log in
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
]
}
dump($this->serializer->serialize($slidingPagination, 'json', ['groups' => 'user']));
/**
* @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;
$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!
/**
* @var string
*
* @ORM\Column(name="slug", type="string", length=255, unique=true)
*
* @Groups({"user"})
*/
private $slug;
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question