Get parent_id of Gedmo Tree element using symfony3, doctrine2 and stofDoctrineExtensionsBundle
<h2>Introduction</h2>

<p>I am using Symfony v3.1.6, Doctrine v2.5.4 and StofDoctrineExtensionsBundle [1] in order to manage Tree structure.</p>

<p>To setup Tree structure I used documentation on Symfony.com [2] followed by documentation on GitHub [3]. Then I proceeded with tree setup - used tree entity from example [4] and used code in [5] to create a tree.</p>

<ul>
<li>[1] <a href="https://github.com/stof/StofDoctrineExtensionsBundle" rel="nofollow noreferrer">stofDoctrineExtensionsBundle on GitHub</a>;</li>
<li>[2] <a href="http://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html" rel="nofollow noreferrer">stofDoctrineExtensinsBundnle documentation on Symfony.com</a>;</li>
<li>[3] <a href="https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md" rel="nofollow noreferrer">Gedmo Tree documentation on GitHub</a>;</li>
<li>[4] <a href="https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#tree-entity-example" rel="nofollow noreferrer">Gedmo Tree > Tree Entity example</a>;</li>
<li>[5] <a href="https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#basic-usage-examples" rel="nofollow noreferrer">Gedmo Tree > Basic Usage Example</a>;</li>
</ul>

<h2>Code</h2>

<p>Function in <code>CategoryRepository</code></p>

<pre><code>public function getParentId($child_node_id)
{
    // get parent_id of a tree element
    $em = $this-&gt;getEntityManager();
    $query = $em-&gt;createQueryBuilder()
        -&gt;select('c')
        -&gt;from('AppBundle:Category', 'c')
        -&gt;where('c.id = :nodeId')
        -&gt;setParameter('nodeId', $child_node_id)
        -&gt;setMaxResults(1)
        -&gt;getQuery();

    $parent = $query-&gt;getResult();
    $parent_null = $parent[0]-&gt;getParent();

    // if parent element is null - efectively element is root of the tree
    if ($parent_null === null)
    {
        $parent_id = $parent[0]-&gt;getId();
    }
    // if parent element is not root of the tree
    else
    {
        $parent_id = $parent[0]-&gt;getParent()-&gt;getId();
    }

    return $parent_id;
}
</code></pre>

<p>When I use <code>$parent = $query-&gt;getResult()</code> I get following output:</p>

<pre><code>array:1 [?
  0 =&gt; Category {#581 ?
    -id: 3
    -title: "Vegetables"
    -is_file: false
    -lft: 4
    -lvl: 1
    -rgt: 11
    -root: Category {#518 ?}
    -parent: Category {#518 ?
      -id: 1
      -title: "Food"
      -is_file: false
      -lft: 1
      -lvl: 0
      -rgt: 12
      -root: Category {#518}
      -parent: null
      -children: 
</code></pre>

<p>but when I use <code>$parent = $query-&gt;getArrayResult()</code> I get:</p>

<pre><code>array:1 [?
  0 =&gt; array:6 [?
    "id" =&gt; 2
    "title" =&gt; "Fruits"
    "is_file" =&gt; false
    "lft" =&gt; 2
    "lvl" =&gt; 1
    "rgt" =&gt; 3
  ]
]
</code></pre>

<p>Note that there is no parent_id in the <code>getArrayResult</code> output...</p>

<h2>Question</h2>

<p>So the question is - how can one get tree node <code>parent_id</code> in the query result with <code>getArrayResult</code> ?</p>

<h2>Conclusion</h2>

<p>Please advise.</p>

<p>Thank you for your time and knowledge.</p>