如何使用symfony3,doctrine2和stofDoctrineExtensionsBundle获取Gedmo Tree元素的ParentId

时间:2016-10-31 18:22:16

标签: symfony doctrine-orm tree doctrine-extensions stofdoctrineextensions

简介

我正在使用Symfony v3.1.6,Doctrine v2.5.4和StofDoctrineExtensionsBundle [1]来管理树结构。

要设置树结构,我在Symfony.com上使用了文档[2],然后是关于GitHub的文档[3]。然后我继续进行树设置 - 使用示例[4]中的树实体,并使用[5]中的代码创建树。

问题

我想为树的每个元素处理parent_id,我无法弄清楚如何正确地完成它。

守则

控制器

/**
 * @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));
}

构建树数据数组

$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();

当我转储$build_my_tree时,我得到:

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...

但我想得到以下内容:

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...

每个树元素中都是parent_id

结论

请告知。

感谢您的时间和知识。

1 个答案:

答案 0 :(得分:1)

当使用getArrayResult时,必须提示获取所有相关值。

Vanilla回答[1]对我不起作用,所以我做了一点修改(查询的完整路径)。

工作代码:

$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();

请注意,提示正在$query上进行,并且位于查询和获取结果之间。

链接:

  1. Found initial info here
  2. Documentation on hints