Doctrine NestedSet Navigation Book

I have a model that implements the behavior of NestedSet:

Page: actAs: NestedSet: hasManyRoots: true rootColumnName: root_id columns: slug: string(255) name: string(255) 

Examples of fixtures:

 Page: NestedSet: true Page_1: slug: slug1 name: name1 Page_2: slug: slug2 name: name2 children: Page_3: slug: page3 name: name3 

I am looking for the easiest way to implement trail navigation. For example, for Page_3, the navigation would look like this:

 <a href="page2">name2</a> > <a href="page2/page3>name3</a> 
0
source share
3 answers

Almost the same as in another question, but you need to add parentUrl variable:

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

Give the root of your tree as $node (hydrate it hierarchically), the node of the current page as $pageNode and '' as $currentNodeUrl and add a '>' and a link to the current page.

Why is this solution using recursion rather than getAncestors() ? Since your urls seem to imply recursion.

0
source

Since I hate having any logic in the templates (and partial), here is my slightly improved version.

 //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; ?> 

So, all the logic for constructing the URL is now in the Page :: getPath () method.

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

What I don't like when I need to pass $ parent to the :: getPath () page. It just does not make semantic meaning.

+1
source

Another answer, simpler (and possibly 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 it with an array of ancestor nodes, or find a way to put a Doctrine_Collection if you want to use it with getAncestors() directly. Again, your whole problem arises from the fact that your URLs are recursively computed, it would be easier and faster to display if you had a colum path with the current URL (but then you have to calculate, update it) and t .d. think about it if you have more readings than records (if your tree does not change often).

0
source

All Articles