JMSSerializer exclusion strategies can help you.
First, exclude all default properties:
// ... use JMS\Serializer\Annotation as JMS; /** * @ORM\Entity * @JMS\ExclusionPolicy("all") */ class Foo
Then choose to exclude or publish your properties:
protected $name;
In addition, for your associations, you can use MaxDepth to limit the related records of your results. Example:
// Entity /** @MaxDepth(1) */ private $selfAssociatedEntries; // Controller use JMS\Serializer\SerializationContext; $serializer->serialize($data, 'json', SerializationContext::create()->enableMaxDepthChecks());
Similarly, your object will contain a series of $selfAssociatedEntries , but $selfAssociatedEntries does not have the $selfAssociationEntries .
Serialization is limited to the first parent.
Groups is a powerful feature that allows you to set properties for some actions and exclude them for another:
/** * @ORM\Column(type="string", length=255, nullable=true, unique=false) * @JMS\Groups({"default"}) // Set the group to expose the property */ protected $name;
In your controller, set the group used for serialization:
// Property 'name' is exposed $serializer->serialize($data, 'json', SerializationContext::create()->setGroups(array('default')));
See the "Exclusion Strategies" in the documentation for more information.