Provides Doctrine proxies handler for JMS Serializer to disable lazy-loading and recursion during serialization.
MIT License
Provides Doctrine proxies handler for JMS Serializer to disable lazy-loading and recursion during serialization.
This library solves problems discussed on Stack Overflow, in this thread, or this one, and is inspired by this gist.
Using Composer:
{
"require": {
"alcalyn/serializer-doctrine-proxies": "1.x"
}
}
You have to add a handler and a listener to your Serializer instance. Here using SerializerBuilder:
use Alcalyn\SerializerDoctrineProxies\DoctrineProxyHandler;
use Alcalyn\SerializerDoctrineProxies\DoctrineProxySubscriber;
use JMS\Serializer\EventDispatcher\EventDispatcher;
use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\SerializerBuilder;
$serializer = SerializerBuilder::create()
->addDefaultHandlers() // This line to avoid to default handlers to be overrided by the new one.
->configureHandlers(function (HandlerRegistryInterface $handlerRegistry) {
$handlerRegistry->registerSubscribingHandler(new DoctrineProxyHandler());
})
->configureListeners(function (EventDispatcher $dispatcher) {
$dispatcher->addSubscriber(new DoctrineProxySubscriber(false)); // false to disable lazy loading.
})
->build()
;
$serializer->serialize($myEntityWithABunchOfRelationsIDontWantToLazyLoadDuringSerialization);
This project is under MIT.