Php 使用doctrine2嵌套集围绕li标记包装分层数据

Php 使用doctrine2嵌套集围绕li标记包装分层数据,php,symfony,doctrine-orm,hierarchical-data,Php,Symfony,Doctrine Orm,Hierarchical Data,我正在使用管理我的分层数据 它使用以下数据库结构管理层次结构: categories -id -root -lft -rgt -name 我需要用li标记包装一个节点,如下所示: Vehicles Bikes Pulsor Hero Honda Automobiles Trucks Vehicles Bikes Pulsor Hero Honda 250 cc

我正在使用管理我的分层数据

它使用以下数据库结构管理层次结构:

 categories
 -id
 -root
 -lft
 -rgt
 -name
我需要用li标记包装一个节点,如下所示:

 Vehicles
    Bikes
       Pulsor
       Hero Honda
    Automobiles
    Trucks
Vehicles
    Bikes
       Pulsor
       Hero Honda
          250 cc
       Automobiles
       Trucks
此捆绑包提供了以下操作节点的方法:

$tree=fetchTreeAsArray($nodeId);  //fetches tree for that node
$node->getNumberDescendants();    //returns all descendants for that node
有关方法的更多说明,请访问

我想将节点环绕在li标记周围:

到目前为止,我试过:

         $tree = $nsm->fetchTreeAsArray(8);
    $treeLiTags="<ul>";
    foreach ($tree as $node) {
        $treeLiTags.="<li>".$node;
        if ($node->hasChildren()) {
            echo $node->getNumberDescendants();
            $treeLiTags.="<ul>";
            $closeParent=true;
        }
        else {
            if ($closeParent && !$node->hasNextSibling()) {
                $closeParent=false;
                $treeLiTags.="</ul>";
            }
            $treeLiTags.="</li>";
        }
    }
    $treeLiTags.="</ul>";
    echo $treeLiTags;
我应该得到:

Vehicles
   Bikes
      Pulsor
      Hero Honda
         250 cc
   Automobiles
   Trucks
任何算法都会有帮助吗?

您考虑过使用的实现吗

它已经实现了您想要实现的目标-您只需使用:

$repo = $em->getRepository('Entity\Category');
$options = array(
    'decorate' => true,
    'rootOpen' => '<ul>',
    'rootClose' => '</ul>',
    'childOpen' => '<li>',
    'childClose' => '</li>',
    'nodeDecorator' => function($node) {
        return '<a href="/page/'.$node['slug'].'">'.$node[$field].'</a>';
    }
);
$htmlTree = $repo->childrenHierarchy(
    null, /* starting from root nodes */
    false, /* load all children, not only direct */
    $options
);
$repo=$em->getRepository('Entity\Category');
$options=array(
“装饰”=>正确,
“rootOpen”=>“
    ”, “rootClose”=>“
”, “childOpen”=>“
  • ”, “childClose”=>“
  • ”, 'NodeCorator'=>函数($node){ 返回“”; } ); $htmlTree=$repo->childrenHierarchy( null,/*从根节点开始*/ false,/*加载所有子项,而不仅仅是直接加载*/ 美元期权 );
    感谢您提供指向嵌套集合树read me文件的链接。我想我会试试这个分机