NestedSet - parent / child nodes

I'm currently working on releasing a hierarchy in terms of the navigation menu from nestedSet to Doctrine.

I have several parents who have several children.

Currently, there are only 2 levels: Parent and Child (no grandchildren).

I have the following code:

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

This outputs:

 Parent Node 1 Child Node 1 Child Node 2 Parent Node 2 Child Node 3 

It's great.

The problem is that I would like my URLs to match the relationship between parents and children.

So the URL for Child Node 2 will be /parent-node-1/child-node-2 (it's like slug ).

Therefore, any parent child must have parent Node traffic.

I hope this makes sense?

thanks

0
source share
1 answer

Well, one way to use this is to use Doctrine_Core::HYDRATE_RECORD_HIERARCHY , which allows you to call methods on your nodes.

Now you can create your own method for node:

 class Model extends BaseModel { public function __toString() { return $this->getSlug(); } public function getUrl() { //getPath uses __toString method to render a node $url = $this->getNode()->getPath('/', true); return $url; } } 

And name it in the template as follows:

 <a href="<?php echo $node->getUrl() ?>"><?php echo $node['name'] ?></a> 
+1
source

All Articles