【发布时间】:2011-05-13 16:05:18
【问题描述】:
我目前正在根据 Doctrine 中的嵌套集的导航菜单输出层次结构。
我有很多父母,然后有几个孩子。
目前只有 2 个级别:Parent 和 Child(没有孙子)。
我有以下代码:
//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) ?>
这会渲染出来:
Parent Node 1
Child Node 1
Child Node 2
Parent Node 2
Child Node 3
这很棒。
问题是,我希望我的网址匹配父/子关系。
因此,子节点 2 的 URL 将是 /parent-node-1/child-node-2(这些为 slug 字段)。
因此,父节点的任何子节点也需要拥有父节点的路由 slug。
我希望这是有道理的?
谢谢
【问题讨论】:
标签: symfony1 symfony-1.4 doctrine-1.2