Symfony1 面包屑导航与原则嵌套集

Symfony1 面包屑导航与原则嵌套集,symfony1,doctrine,nested-sets,doctrine-1.2,Symfony1,Doctrine,Nested Sets,Doctrine 1.2,我有一个实现嵌套集行为的模型: Page: actAs: NestedSet: hasManyRoots: true rootColumnName: root_id columns: slug: string(255) name: string(255) 固定装置示例: Page: NestedSet: true Page_1: slug: slug1 name: name1 Page_2: slug:

我有一个实现嵌套集行为的模型:

Page:
  actAs:
    NestedSet:
      hasManyRoots: true
      rootColumnName: root_id
  columns:
    slug: string(255)
    name: string(255)
固定装置示例:

Page:
  NestedSet: true
  Page_1:
    slug: slug1
    name: name1
  Page_2:
    slug: slug2
    name: name2
    children:
      Page_3:
        slug: page3
        name: name3
我正在寻找实现面包屑导航(trail)的最简单方法。例如,对于第3页,导航将如下所示:

<a href="page2">name2</a> > <a href="page2/page3>name3</a>
>

与另一个问题几乎相同,但必须添加一个“parentUrl”变量:

//module/templates/_breadcrumbElement.php
foreach ($node->get('__children') as $child) :
  if ($child->isAncestorOf($pageNode)):
     $currentNodeUrl = $parentUrl . $child->getSlug() . '/';
     echo link_to($child->getName(), $currentNodeUrl) . ' > ' ;
     include_partial('module/breadcrumbElement', array('node' => $child, 'pageNode' => $pageNode, 'parentUrl' => $currentNodeUrl));
  endif;
endforeach;
$node
的形式将其作为树的根节点提供(按层次进行),以
$pageNode
的形式将其作为当前页面的节点提供,以
$currentNodeUrl
的形式将其作为“”提供,并将“>”和链接添加到当前页面


为什么这个解决方案使用递归而不是
get祖先()
?因为您的URL似乎意味着递归。

另一个答案,更简单(也许更有效),使用get祖先()和递归:

//module/templates/_breadcrumbElement.php
if ($node = array_pop($nodes)) // stop condition
{
    $currentNodeUrl = $parentUrl . $node->getSlug() . '/';
    echo link_to($node->getName(), $currentNodeUrl) . ' > ' ;
    include_partial('module/breadcrumbElement', array(
      'nodes' => $nodes, 'parentUrl' => $currentNodeUrl));
}
使用祖先节点数组调用此函数,或者如果要直接将其与
get祖先()
一起使用,请找到一种方法来弹出
Doctrine\u集合。

同样,您所有的问题都来自这样一个事实,即您的url是递归计算的,如果您有一个包含当前url的列路径(但是您必须计算、更新它),那么显示会更简单、更快,等等。。。如果你有更多的读比写(如果你的树不经常改变),请考虑这样做。

< P>因为我讨厌在模板(和部分)中有任何逻辑,这是我稍微改进的版本。
//module/templates/_breadcrumbElement.php
<?php foreach ($node as $child): ?>
<li>
  <a href="<?php echo $child->getPath($parent) ?>"><?php echo $child->getName() ?></a>
  <?php if (count($child->get('__children')) > 0): ?>
    <ul>
      <?php include_partial('node', array('node' => $child->get('__children'), 'parent' => $child)) ?>
    </ul>
  <?php endif; ?>
</li>
<?php endforeach; ?>

我不喜欢的是必须将$parent传递给Page::getPath()。它没有任何语义上的意义。

你在第1页和第2页的段塞中没有犯错误吗?不应该是第1页和第2页吗?很好,递归就是这样。非常感谢。
class Page extends BasePage
{
  /**
   * Full path to node from root
   *
   */
  protected $path = false;

  public function __toString()
  {
    return $this->getSlug();
  }
  public function getPath($parent = null)
  {
    if (!$this->path)
    {
      $this->path = join('/', null !== $parent ? array($parent->getPath(), $this) : array($this));
    }
    return $this->path;
  } 
}