Answer the question
In order to leave comments, you need to log in
Symfony serialize stops working when adding a certain connection, why?
Hello everyone, there are 3 tables, user, ticket, ticket_assigned_user, ticket has a relationship with ticket_assigned_user, this relationship is OneToMany. But if I add a OneToMany connection between user and ticket_assigned_user, then the serializer leaves so that the page crashes with a timeout error. What exactly is the problem here?
$encoders = [new XmlEncoder(), new JsonEncoder()];
$classMetadataFactory = new ClassMetadataFactory(new XmlFileLoader('/var/www/new-sys.local/config/packages/serialization/definition.xml'));
$defaultContext = [
AbstractObjectNormalizer::MAX_DEPTH_HANDLER => function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
return $innerObject->id;
},
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
return $object->getId();
},
];
$normalizers = [
new DateTimeNormalizer(),
new ObjectNormalizer(
$classMetadataFactory,
null,
null,
null,
null,
null,
$defaultContext
),
];
$serializer = new Serializer($normalizers, $encoders);
$tickets = $this->em->getRepository(Ticket::class)->findBy([], null, 10);
$jsonContent = $serializer->serialize($tickets, 'json', [
AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true,
]);
Answer the question
In order to leave comments, you need to log in
Since you do not just refer to the first class to the second and the second to the first, but through the third entity, Doctrine does not process them correctly. In such cases, it is worth removing the output of the user.assignedTickets field , just through the array you mentioned
[AbstractNormalizer::ATTRIBUTES => ['assignedUsers' => ['assignedTickets']]
, then the structure will be flat, but without going into recursion.Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question