title: Render Doctrine entities including recursive relations as JSON
body: <p>I am building a JSON-based REST API using Symfony2.4 with Doctrine2.</p>
  
  <p>EDIT : with JsonNormalizer, I can disabled some attributes, but what if I would like to set them, without recursivity ?</p>
  
  <p>Basically, what I have (working) now is :</p>
  
  <pre><code>{
      "tasks": [
          {
              "id": 1,
              "name": "first task"
          },
          {
              "id": 2,
              "name": "second task"
          }
      ]
  }
  </code></pre>
  
  <p>What I would like is :</p>
  
  <pre><code>{
      "tasks": [
          {
              "id": 1,
              "name": "first task",
              "category": {
                  "id": 1,
                  "name": "first category"
              }
          },
          {
              "id": 2,
              "name": "second task",
              "category": {
                  "id": 1,
                  "name": "first category"
              }
          }
      ]
  }
  </code></pre>
  
  <p>What was my initial problem is :</p>
  
  <pre><code>{
      "tasks": [
          {
              "id": 1,
              "name": "first task",
              "category": {
                  "id": 1,
                  "name": "first category",
                  "tasks": [
                      {
                          "id": 1,
                          "name": "first task",
                          "category": {
                              "id": 1,
                              "name": "first category",
                              "tasks": [...] // infinite...
                          }
                      },
                      {
                          "id": 2,
                          "name": "second task",
                          "category": {
                              "id": 1,
                              "name": "first category",
                              "tasks": [...] // infinite...
                          }
                      }
                  ]
              }
          },
          {
              "id": 2,
              "name": "second task",
              "category": {
                  "id": 1,
                  "name": "first category",
                  "tasks": [
                      {
                          "id": 1,
                          "name": "first task",
                          "category": {
                              "id": 1,
                              "name": "first category",
                              "tasks": [...]
                          }
                      },
                      {
                          "id": 2,
                          "name": "second task",
                          "category": {
                              "id": 1,
                              "name": "first category",
                              "tasks": [...]
                          }
                      }
                  ]
              }
          }
      ]
  }
  </code></pre>
  
  <p>I have a entity A with a manyToOne relation to another entity B.</p>
  
  <p><strong>I have implemented the reverse-side, to be able to retrieve the related A entities on the B one.</strong></p>
  
  <pre><code>class Task
  {
      /**
       * @ORM\ManyToOne(targetEntity="List", inversedBy="task")
       * @ORM\JoinColumn(name="list_id", referencedColumnName="id")
       */
      private $list;
  
      public function toArray($recursive = false)
      {
          $entityAsArray = get_object_vars($this);
  
          if ($recursive) {
              foreach ($entityAsArray as &amp;$var) {
                  if ((is_object($var)) &amp;&amp; (method_exists($var, 'toArray'))) {
                      $var = $var-&gt;toArray($recursive);
                  }
              }
          }
  
          return $entityAsArray;
      }
  }
  
  use Doctrine\Common\Collections\ArrayCollection;
  
  class List
  {
      /**
       * @ORM\OneToMany(targetEntity="Task", mappedBy="list")
       */
      private $tasks;
  
      public function __construct()
      {
          $this-&gt;tasks = new ArrayCollection();
      }
  }
  </code></pre>
  
  <p>Then I am building the different API routes and controllers,
  Rendering the output as JsonResponses,
  And I would like to render, for a given list, the different tasks using the route :</p>
  
  <pre><code>/api/v1/lists/1/tasks
  </code></pre>
  
  <p>The task action of my controller :</p>
  
  <pre><code>public function tasksAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $list = $em-&gt;getRepository('MyRestBundle:List')-&gt;findOneActive($id);
  
      if (!$list) {
          throw $this-&gt;createNotFoundException('List undefined');
      }
  
      $tasks = $list-&gt;getTasks()-&gt;toArray();
  
      foreach ($tasks as &amp;$task) {
          // recursively format the tasks as array
          $task = $task-&gt;toArray(true);
      }
  
      $serializer = $this-&gt;get('serializer');
  
      return $this-&gt;generateJsonResponse($serializer-&gt;normalize($tasks), 200);
  }
  </code></pre>
  
  <p>But unfortunately, I always get a memory leak, because the call of toArray() is recursive, so each task has a list property which has a tasks collection etc.</p>
  
  <p>PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 130968 bytes) in src/Symfony/Component/Serializer/Serializer.php on line 146</p>
  
  <p>I am wondering what would be the cleanest way to render entities with relations as JSON objects with Symfony2 ?</p>
  
  <p>Do I really have to loop on my tasks to execute the "toArray()" method ?</p>
  
  <p>I have also tried without it, without more success, except that the leak in in the file : src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php...</p>
  
  <p>I have also tried without the JMSSeralizer, and the memory leak is thrown in my own php file.</p>
  
  <p>Of course, I could increase the memory limit, but as it is an infinite loop problem of toArray() calls, it will not solve my problem.</p>
  
  <p>How to format it properly ?</p>