I'm building an API for a Symfony2 project using FOSRestBundle, and I often simply return Doctrine objects to be encoded in JSON. Typically this way :
/**
* @Rest\View()
*/
public function getEventsAction(Request $request)
{
// security checks
return $this->getDoctrine()->getRepository('SomeBundle:Event')->findAll();
}
Thing is, this returns all object properties and relationships, and in many cases I don't want that, for example with a User object that contains the hashed password and everything.
Is there a way to set up automatic filters when encoding Doctrine objects in JSON ? Or do I have to create a QueryBuilder only fetching required data ?