title: Doctrine2 One-To-Many, Self-referencing relationship
body: <p>In my 'Topic' entity, I have a One-To-Many, Self-referencing relationship <code>$parent:$children</code>.</p>
  
  <pre><code>class Topic
  {
      /** @ORM\Id 
      * @Column(type="integer")
      * @ORM\GeneratedValue(strategy="IDENTITY")
      */
      private $id;
  
      /** @Column(length=40, unique=true) */
      private $name;
  
      /**
       * @ORM\ManyToOne(targetEntity="Topic", inversedBy="children")
       */
      private $parent;
  
      /**
       * @ORM\OneToMany(targetEntity="Topic", mappedBy="parent")
       */
      private $children;
  }
  </code></pre>
  
  <p>I can join the table to get the parent-children hierarchy like this:</p>
  
  <pre><code>return $this-&gt;getEntityManager()-&gt;createQuery('
      SELECT t, c FROM My\xxxBundle\Entity\Topic t 
      LEFT JOIN t.children c
      WHERE t.parent IS NULL
  ')
  -&gt;getArrayResult();
  </code></pre>
  
  <p>Here's the correct output:</p>
  
  <pre><code>array
    0 =&gt; 
      array
        'id' =&gt; int 1
        'name' =&gt; string 'Parent 1'
        'slug' =&gt; string 'p-1'
        'description' =&gt; null
        'children' =&gt; 
          array
            0 =&gt; 
              array
                  'id' =&gt; int 2
                  'name' =&gt; string 'Child 1-1'
                  'slug' =&gt; string 'c-1-1'
                  'description' =&gt; null
            1 =&gt; 
              array
                  'id' =&gt; int 3
                  'name' =&gt; string 'Child 1-2'
                  'slug' =&gt; string 'c-1-2'
                  'description' =&gt; null
    1 =&gt; 
      array
        'id' =&gt; int 4
        'name' =&gt; string 'Parent 2'
        'slug' =&gt; string 'p-2'
        'description' =&gt; null
        'children' =&gt; 
          array
            empty
  ...
  </code></pre>
  
  <p>but if I try to fetch specific columns in the SELECT statement:</p>
  
  <p><code>SELECT t.name, c.name FROM My\xxxBundle\Entity\Topic t</code></p>
  
  <p>I get a flat array of child entities i.e only <code>c.name</code>. If a parent has no children, I just get a null value for its name:</p>
  
  <pre><code>  1 =&gt; 
      array (size=1)
        'name' =&gt; string 'Child 1-1' (length=14)
    2 =&gt; 
      array (size=1)
        'name' =&gt; string 'Child 1-2' (length=14)
    3 =&gt; 
      array (size=1)
        'name' =&gt; null
    4 =&gt; 
      array (size=1)
        'name' =&gt; string 'Child 3-1' (length=5)
  </code></pre>
  
  <p>On Mark's suggestion, I've renamed the name field of child entity:</p>
  
  <pre><code>SELECT t.name, c.name AS child_name FROM My\xxxBundle\Entity\Topic t
  </code></pre>
  
  <p>but I still get the wrong format:</p>
  
  <pre><code>array
    0 =&gt; 
      array
        'name' =&gt; string 'Parent 1'
        'child_name' =&gt; string 'Child 1-1'
    1 =&gt; 
      array
        'name' =&gt; string 'Parent 1'
        'child_name' =&gt; string 'Child 1-2'
    2 =&gt; 
      array
        'name' =&gt; string 'Parent 2'
        'child_name' =&gt; string 'Child 2-1'
  </code></pre>