coosos/jms-serializer-bidirectional-relation
Adds JMS Serializer subscribers that embed a _mapping_bidirectional_relation in serialized data so bidirectional associations can be restored on deserialize. Supports Symfony or standalone setup via event subscribers, with annotations to enable mapping on root models and exclude fields.
Build a mapping for restore bidirectional relation when deserialize process.
This mapping is add to your serialized content with _mapping_bidirectional_relation key.
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\EventDispatcher\EventDispatcher;
use Coosos\BidirectionalRelation\EventSubscriber\MapDeserializerSubscriber;
use Coosos\BidirectionalRelation\EventSubscriber\MapSerializerSubscriber;
$builder = SerializerBuilder::create();
$builder->configureListeners(function (EventDispatcher $dispatcher) {
$dispatcher->addSubscriber(new MapSerializerSubscriber());
$dispatcher->addSubscriber(new MapDeserializerSubscriber());
});
$serializer = $builder->build();
# services.yml
Coosos\BidirectionalRelation\EventSubscriber\MapDeserializerSubscriber:
tags:
- { name: jms_serializer.event_subscriber }
Coosos\BidirectionalRelation\EventSubscriber\MapSerializerSubscriber:
tags:
- { name: jms_serializer.event_subscriber }
For to avoid mapping on an object that does not need it, you should add
@Coosos\BidirectionalRelation\Annotations\SerializerBidirectionalRelation to annotation class (only root model)
If you want to exclude an object to map,
you can by adding @Coosos\BidirectionalRelation\Annotations\ExcludeFromMapping annotation to your field.
How can I help you explore Laravel packages today?