-1

Introduction

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

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.

Problem

I want to process parent_id for each element of the tree and I can not figure out how to do it properly.

The Code

controller

/**
 * @Route("/tree12", name="tree12")
 */
public function tree12Action(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $repo = $em->getRepository('AppBundle:Category');

    $rootId = 19;
    $query = $em
        ->createQueryBuilder()
        ->select('node')
        ->from('AppBundle:Category', 'node')
        ->where('node.root = '. $rootId)
        ->orderBy('node.root, node.lft', 'ASC')
        ->getQuery();

    $build_my_tree = $query->getArrayResult();

    $ultra = $this->get('app.ultrahelpers');
    $build_my_tree = $ultra->prepareTreeData($build_my_tree);

    //var_dump($build_my_tree);

    $options = array(
        'decorate' => true,
        'rootOpen' => '<ul>',
        'rootClose' => '</ul>',
        'childOpen' => function($node)
        {
            if (array_key_exists('assigned_root_node', $node))
            {
                if ($node['assigned_root_node'] === true)
                {
                    return '<li data-jstree=\'{"type":"root"}\'>';
                }
            }
            else if ($node['is_file'] === true)
            {
                return '<li data-jstree=\'{"type":"file"}\'>';
            }
            else if ($node['is_file'] === false)
            {
                if ($node['title'] === 'Saursliezu_dzelzcels')
                {
                    return '<li data-jstree=\'{"type":"home"}\'>';
                }
                else
                {
                    return '<li data-jstree=\'{"type":"folder"}\'>';
                }
            }
        },
        'childClose' => '</li>',
        'nodeDecorator' => function($node) use ($repo)
        {
            dump($node);
            if (array_key_exists('assigned_root_node', $node))
            {
                if ($node['assigned_root_node'] === true)
                {
                    $link_class = 'magenta';
                }
                //$parent_node_id = $node['parent_id'];
                //$parent_node_id = $repo->getParentId($repo->findOneBy(array('id' => $node['id'])));
            }
            else if ($node['is_file'] === true)
            {
                $link_class = 'blue';
                if ($node['title'] === 'aaa.txt')
                {
                    $link_class = 'red';
                }
                else if ($node['title'] === 'bbb.txt')
                {
                    $link_class = 'green';
                }
                //$parent_node_id = $node['parent_id'];
                $parent_node_id = $repo->getParentId($repo->findOneBy(array('id' => $node['id'])));
            }
            else if ($node['is_file'] === false)
            {
                if ($node['title'] === 'Saursliezu_dzelzcels')
                {
                    $link_class = 'red';
                }
                else
                {
                    $link_class = 'black';
                }
                //$parent_node_id = $node['parent_id'];
                $parent_node_id = $repo->getParentId($repo->findOneBy(array('id' => $node['id'])));;
            }
            return '<a data-parent-id="'. $parent_node_id .'" class="'. $link_class .'" href="/project_path/">'. $node['title'] .'</a>';
        }
    );

    $tree = $repo->buildTree($build_my_tree, $options);
    var_dump($tree);

    return $this->render('tree/tree12_show.html.twig', array('tree' => $tree));
}

Building tree data array

$rootId = 19;
$query = $em
    ->createQueryBuilder()
    ->select('node')
    ->from('AppBundle:Category', 'node')
    ->where('node.root = '. $rootId)
    ->orderBy('node.root, node.lft', 'ASC')
    ->getQuery();

$build_my_tree = $query->getArrayResult();

When I dump $build_my_tree I get:

array (size=6)
  0 => 
    array (size=7)
      'id' => int 1
      'title' => string '&nbsp;Food' (length=10)
      'is_file' => boolean false
      'lft' => int 1
      'lvl' => int 0
      'rgt' => int 12
      'assigned_root_node' => boolean true
  1 => 
    array (size=6)
      'id' => int 2
      'title' => string 'Fruits' (length=6)
      'is_file' => boolean false
      'lft' => int 2
      'lvl' => int 1
      'rgt' => int 3
  2 => 
    array (size=6)
      'id' => int 3
      'title' => string 'Vegetables' (length=10)
      'is_file' => boolean false
      'lft' => int 4
      'lvl' => int 1
      'rgt' => int 11
  3 => 
    array (size=6)
      'id' => int 4
      'title' => string 'Carrots' (length=7)
      'is_file' => boolean false
      'lft' => int 5
      'lvl' => int 2
      'rgt' => int 6
etc...

But I would like to get the following:

array (size=6)
  0 => 
    array (size=7)
      'id' => int 1
      'title' => string '&nbsp;Food' (length=10)
      'is_file' => boolean false
      'lft' => int 1
      'lvl' => int 0
      'rgt' => int 12
      'assigned_root_node' => boolean true
      'parent_id' => int 0
  1 => 
    array (size=6)
      'id' => int 2
      'title' => string 'Fruits' (length=6)
      'is_file' => boolean false
      'lft' => int 2
      'lvl' => int 1
      'rgt' => int 3
      'parent_id' => int 1
  2 => 
    array (size=6)
      'id' => int 3
      'title' => string 'Vegetables' (length=10)
      'is_file' => boolean false
      'lft' => int 4
      'lvl' => int 1
      'rgt' => int 11
      'parent_id' => int 1
  3 => 
    array (size=6)
      'id' => int 4
      'title' => string 'Carrots' (length=7)
      'is_file' => boolean false
      'lft' => int 5
      'lvl' => int 2
      'rgt' => int 6
      'parent_id' => int 3
etc...

That is parent_id in each tree element.

Conclusion

Please advise.

Thank you for your time and knowledge.

Rikijs
  • 728
  • 1
  • 12
  • 48

1 Answers1

1

Turns out one has to hint to get all the related values when using getArrayResult.

Vanilla answer [1] did not work for me, so I made little modification (full path to Query).

working code:

$query = $em
    ->createQueryBuilder()
    ->select('node')
    ->from('AppBundle:Category', 'node')
    ->where('node.root = '. $rootId)
    ->orderBy('node.root, node.lft', 'ASC')
    ->getQuery();

$query->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);
$build_my_tree = $query->getArrayResult();

Note, that hinting is going on on the $query and is placed between query and getting the result.

Links:

  1. Found initial info here
  2. Documentation on hints
Community
  • 1
  • 1
Rikijs
  • 728
  • 1
  • 12
  • 48