alcalyn/serializer-doctrine-proxies

Provides Doctrine proxies handler for JMS Serializer to disable lazy-loading and recursion during serialization.

1.1.2 2016-06-01 21:07 UTC

This package is not auto-updated.

Last update: 2024-11-09 19:17:05 UTC


README

Latest Stable Version Total Downloads 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.

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.