Symfony1 symfony-嵌套集和父/子节点的渲染

Symfony1 symfony-嵌套集和父/子节点的渲染,symfony1,symfony-1.4,Symfony1,Symfony 1.4,我已经使用Symfony1.4中的嵌套集行为原则创建了一个模型,因为我正在尝试创建一个基本的cms,带有继承人的页面 我有几个页面,有父节点和子节点 Parent_1 Child_1 Child_2 Parent_2 Child_3 我的问题是,根据导航标题呈现这些项目。(等) 最简单/最好的方法是什么 我希望根节点具有URL,如/parent\u 1和后续子节点,即parent\u 1/child\u 1 谢谢我编写了一个递归函数,可以从任何节点开始绘制树。指定根节点将绘制

我已经使用Symfony1.4中的嵌套集行为原则创建了一个模型,因为我正在尝试创建一个基本的cms,带有继承人的页面

我有几个页面,有父节点和子节点

Parent_1
   Child_1
   Child_2
Parent_2
   Child_3
我的问题是,根据导航标题呈现这些项目。(
等)

最简单/最好的方法是什么

我希望
根节点
具有URL,如
/parent\u 1
和后续子节点,即
parent\u 1/child\u 1


谢谢

我编写了一个递归函数,可以从任何节点开始绘制树。指定根节点将绘制整个树。它在我的购物车插件中使用,您可以查看已完成UI的演示

我已经粘贴了下面的函数,但是从我的实现中对其进行了修改,使其更加清晰

<?php
//Render a nested set.  Recurses for all descendants of that node. Can be used to draw entire tree, when specifying root id.
//Secondary parameter ($node) is used for performance boost, internally in function.
public static function display_node($id, $node = null) {

    $isRoot = false;

    if ($node == null) {
        $node = Doctrine_Core::getTable('YOURNESTEDTABLENAME')->findOneById($id)->getNode();
        if ($node->isRoot()) {
            $isRoot = true;
        }
    }
    $record = $node->getRecord();

    if (!$isRoot) {
        echo "<li class='tree_item' id='list_". $record->getId() . "'>"; 
        echo "<div class='listitem' id='listitem_".$record->getId()."'>";
        echo $record->getName();
        echo "<div style='clear:both'></div>";
        echo "</div>";

        if ($node->hasChildren()) {
            echo "<ol>";
            foreach ($node->getChildren() as $child) {
                self::display_node($child->getId(), $child->getNode());
            }
            echo "</ol>";
        }
    }
    else {
        if ($node->hasChildren()) {
            echo "<ol class='sortable'>";
            echo "<li class='tree_item root_item' style='position: relative;' id='list_". $record->getId() . "'>";
            foreach ($node->getChildren() as $child) {
                self::display_node($child->getId(), $child->getNode());
            }
            echo "</ol>";
        }
    }
}
?>

我讨厌在模板以外的任何地方回显视图元素,所以这里是我的版本

//actions:
    public function executeShow(sfWebRequest $request)
    {
      $this->tree = Doctrine::getTable('Model')->getMenuTree();
    }

//lib:
    class ModelTable extends Doctrine_Table
    {
      /**
       * Gets tree element in one query
       */
      public function getMenuTree()
      {

        $q = $this->createQuery('g')
          ->orderBy('g.root_id')
          ->addOrderBy('g.lft')
          ->where('g.root_id NOT NULL');

        return $q->execute(array(),  Doctrine_Core::HYDRATE_ARRAY_HIERARCHY);
      }
    }

//template:

<?php function echoNode($tree, $parent=null) { ?>
  <ul>
  <?php foreach ($tree as $node): ?>
    <li data-id='<?php echo $node['id'] ?>'>
      <?php echo $node['name'] ?>
      <?php if (count($node['__children']) > 0): ?>
        <?php echo echoNode($node['__children'], $node) ?>
      <?php endif; ?>
    </li>
  <?php endforeach; ?>       
  </ul>
<?php } ?>

<?php echo echoNode($tree) ?>
//操作:
公共函数executeShow(sfWebRequest$request)
{
$this->tree=Doctrine::getTable('Model')->getMenuTree();
}
//库:
类ModelTable扩展了Doctrine\u表
{
/**
*在一个查询中获取树元素
*/
公共函数getMenuTree()
{
$q=$this->createQuery('g')
->orderBy('g.root\u id')
->addOrderBy('g.lft')
->其中('g.root_id NOT NULL');
返回$q->execute(array(),Doctrine\u Core::hydration\u array\u HIERARCHY);
}
}
//模板:

现在,如果您需要树的一部分,您可以在一个动作中完成,或者更好,为此编写一个单独的模型方法。

@Jon谢谢!这对我来说很有意义。我的根节点是
0
。我试图用一个组件从调用此函数,但我有点不确定如何使用
$categoryid
@Jon目前我有:
$categoryid='1'$此->标题=树表::显示节点($categoryid)这似乎只显示一个子节点,而不显示任何其他的父节点或子节点。知道我做错了什么吗?类别是我使用嵌套集设置的“实体”。在您的情况下,它将类似于PageID。你可以按照你想要的方式查询你的嵌套集模型,我只是简单地使用了ID,因为它对于我的每个类别都是唯一的。好的,相同的代码是否会打开菜单:这才是我真正想要的。我有一个nestedset管理模块,只是不像你那样有一个前端导航菜单。我为你清理了一些代码,可能会更清晰一些。我从等式中删除了“假根”节点。该代码与cart.samedraw.com站点不同,但经过修改,因为许多代码都是特定于我的实现的。你可以在这里看到一切。。。或者特别是在这个文件Ok中,条令查询抛出了一个错误,与
where
子句有关。另一个问题是为父节点获取
parent-1
格式的URL,然后为子节点获取
parent-1/child-1
。这可能吗?如果您确定所有对象都是某个树的一部分(root_id不是NULL),那么您可以删除where子句。当您将模型迁移到嵌套集时,可能会出现这种情况。是的,我刚刚将
设置为非NULL
,并且似乎有效。关于路由/URL,有没有一个简单的方法可以做到这一点?当然,这是我的错。我不太明白你关于url的问题。我建议你开始一个单独的问题,更好地解释你需要什么。我提出了一个新问题,对URL/路由进行了更详细的解释: