Breadcrumb navigation with Doctrine NestedSet

前端 未结 3 361
半阙折子戏
半阙折子戏 2021-01-26 15:29

I have a model that implements NestedSet behaviour:

Page:
  actAs:
    NestedSet:
      hasManyRoots: true
      rootColumnName: root_id
  columns:
    slug: str         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 16:22

    Another answer, more simple (and perhaps more efficient), with getAncestors() and recursion:

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

    Call this with an array of ancestor nodes or find a way to pop a Doctrine_Collection if you want to use it with getAncestors() directly. Again, all your problem comes from the fact that your urls are recursively computed, it would be simpler and faster to display if you had a colum path with the current url (but then you would have to compute, update it), etc... consider doing this if you have more reads than writes (if your tree does not change often).

提交回复
热议问题