我通常像这样获取 NestedSet 树:
class ModelTable extends Doctrine_Table
{
/**
* Gets tree elements in one query
*/
public function getMenuTree()
{
$q = $this->createQuery('p')
->orderBy('p.root_id')
->addOrderBy('p.lft');
$tree = $q->execute(array(), Doctrine_Core::HYDRATE_RECORD_HIERARCHY);
return $tree;
}
}
所以我实际上可以在只使用一个查询数据库的同时显示整棵树..直到我尝试遍历树。例如,如果您在这样的节点上调用方法:
$node->getNode()->getAncestors()
Doctrine 将为此建立一个全新的查询(查看 Doctrine_Node_NestedSet::getAncestors())。其他遍历方法如 getChildren() 也使用 DQL。但这有点低效,不是吗?一旦我获取了整棵树,我就不想再查询数据库了。
也许有人写了一个驱动程序来以正确的方式做到这一点?(无 DQL)