alcalyn / serializer-doctrine-proxies
Provides Doctrine proxies handler for JMS Serializer to disable lazy-loading and recursion during serialization.
Installs: 6 963
Dependents: 1
Suggesters: 0
Security: 0
Stars: 9
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=5.5
- doctrine/common: ~2.4
- doctrine/orm: ~2.4
- jms/serializer: ~1.1
This package is not auto-updated.
Last update: 2024-12-21 19:52:59 UTC
README
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.
Installation
Using Composer:
{ "require": { "alcalyn/serializer-doctrine-proxies": "1.x" } }
Usage
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);
License
This project is under MIT.