title: Doctrine - self-referencing entity - disable fetching of children
body: <p>I have a very simple entity(WpmMenu) that holds menu items connected to one another in a self-referencing relationship (adjecent list it's called)?
  so in my entity I have:</p>
  
  <pre><code>protected $id
  protected $parent_id
  protected $level
  protected $name
  </code></pre>
  
  <p>with all the getters/setters the relationships are:</p>
  
  <pre><code>/**
  * @ORM\OneToMany(targetEntity="WpmMenu", mappedBy="parent")
  */
  protected $children;
  
  /**
  * @ORM\ManyToOne(targetEntity="WpmMenu", inversedBy="children", fetch="LAZY")
  * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onUpdate="CASCADE", onDelete="CASCADE")
  */
  protected $parent;
  
  public function __construct() {
     $this-&gt;children = new ArrayCollection();
  }
  </code></pre>
  
  <p>And everything works fine. When I render the menu tree, I get the root element from the repository, get its children, and then loop through each child, get its children and do this recursively until I have rendered each item.</p>
  
  <p>What happens (and for what I am seeking a solution)is this:
  At the moment I have 5 level=1 items and each of these items have 3 level=2 items attached (and in the future I will be using level=3 items as well). To get all elements of my menu tree Doctrine executes:</p>
  
  <ul>
  <li>1 query for the root element + </li>
  <li>1 query to get the 5 children(level=1) of the root element +</li>
  <li>5 queries to get the 3 children(level=2) of each of the level 1 items +</li>
  <li>15 queries (5x3) to get the children(level=3) of each level 2 items</li>
  </ul>
  
  <p>TOTAL: 22 queries</p>
  
  <p>So, I need to find a solution for this and ideally I would like to have 1 query only.</p>
  
  <p><strong>So this is what I am trying to do:</strong>
  In my entities repository(WpmMenuRepository) I use queryBuilder and get a flat array of all menu items ordered by level. Get the root element(WpmMenu) and add "manually" its children from the loaded array of elements. Then do this recursively on children. Doing this way I could have the same tree but with a single query.</p>
  
  <p><strong>So this is what I have:</strong></p>
  
  <p>WpmMenuRepository:</p>
  
  <pre><code>public function setupTree() {
      $qb = $this-&gt;createQueryBuilder("res");
      /** @var Array */
      $res = $qb-&gt;select("res")-&gt;orderBy('res.level', 'DESC')-&gt;addOrderBy('res.name','DESC')-&gt;getQuery()-&gt;getResult();
      /** @var WpmMenu */
      $treeRoot = array_pop($res);
      $treeRoot-&gt;setupTreeFromFlatCollection($res);
      return($treeRoot);
  }
  </code></pre>
  
  <p>and in my WpmMenu entity I have:</p>
  
  <pre><code>function setupTreeFromFlatCollection(Array $flattenedDoctrineCollection){
    //ADDING IMMEDIATE CHILDREN
    for ($i=count($flattenedDoctrineCollection)-1 ; $i&gt;=0; $i--) {
       /** @var WpmMenu */
       $docRec = $flattenedDoctrineCollection[$i];
       if (($docRec-&gt;getLevel()-1) == $this-&gt;getLevel()) {
          if ($docRec-&gt;getParentId() == $this-&gt;getId()) {
             $docRec-&gt;setParent($this);
             $this-&gt;addChild($docRec);
             array_splice($flattenedDoctrineCollection, $i, 1);
          }
       }
    }
    //CALLING CHILDREN RECURSIVELY TO ADD REST
    foreach ($this-&gt;children as &amp;$child) {
       if ($child-&gt;getLevel() &gt; 0) {      
          if (count($flattenedDoctrineCollection) &gt; 0) {
             $flattenedDoctrineCollection = $child-&gt;setupTreeFromFlatCollection($flattenedDoctrineCollection);
          } else {
             break;
          }
       }
    }      
    return($flattenedDoctrineCollection);
  }
  </code></pre>
  
  <p><strong>And this is what happens:</strong></p>
  
  <p>Everything works out fine, BUT I end up with each menu items present twice. ;) Instead of 22 queries now I have 23. So I actually worsened the case.</p>
  
  <p>What really happens, I think, is that even if I add the children added "manually", the WpmMenu entity is NOT considered in-sync with the database and as soon as I do the foreach loop on its children the loading is triggered in ORM loading and adding the same children that were added already "manually".</p>
  
  <p><strong>Q</strong>: Is there a way to block/disable this behaviour and tell these entities they they ARE in sync with the db so no additional querying is needed?</p>