Last active
March 26, 2017 17:20
-
-
Save Keeo/edcc96366921edd3b0b8 to your computer and use it in GitHub Desktop.
JMS serializer bundle visitor example - how to add another field into serialized entity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Serializer; | |
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; | |
use JMS\Serializer\EventDispatcher\ObjectEvent; | |
class ExampleVisitor implements EventSubscriberInterface | |
{ | |
static public function getSubscribedEvents() | |
{ | |
return array( | |
array('event' => 'serializer.post_serialize', 'class' => 'Acme\DemoBundle\Entity\Example', 'method' => 'onPostSerialize'), | |
); | |
} | |
public function onPostSerialize(ObjectEvent $event) | |
{ | |
$event->getVisitor()->addData('someKey','someValue'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
treewer.serializer.company: | |
class: Acme\DemoBundle\Serializer\ExampleVisitor | |
tags: | |
- { name: jms_serializer.event_subscriber } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment