我正在使用stof/StofDoctrineExtensionsBundle(Atlantic18/DoctrineExtensions的捆绑包装器)来实现嵌套集(树)实体。该实体已配置并正在工作,但我无法弄清楚如何在单个查询中检索所有根注释及其所有子项(完整树)。我目前有完整的集合返回,但是它延迟加载所有子项,这意味着执行了大量查询。
谢谢你的帮助。
我正在使用stof/StofDoctrineExtensionsBundle(Atlantic18/DoctrineExtensions的捆绑包装器)来实现嵌套集(树)实体。该实体已配置并正在工作,但我无法弄清楚如何在单个查询中检索所有根注释及其所有子项(完整树)。我目前有完整的集合返回,但是它延迟加载所有子项,这意味着执行了大量查询。
谢谢你的帮助。
You can just use childrenHierarchy()
method for whole tree retrieve:
$tree = $repo->childrenHierarchy();
找到了解决方案。
检索节点对象的完整列表:
$repo = $this->getDoctrine()->getManager()->getRepository('NestedEntity');
$nodes = $repo->getChildren();
用你的节点构建树。
$tree = $repo->getRepoUtils()->buildTreeArray($nodes);
buildTreeArray 方法接受节点数组数组,因此您必须在实体中实现 ArrayAccess 接口。它还将所有子节点放在__children
节点数组的键中。
/**
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="nested_entity")
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
*/
class NestedEntity implements \ArrayAccess
{
public function offsetExists($offset)
{
return property_exists($this, $offset);
}
public function &offsetGet($offset)
{
return $this->$offset;
}
public function offsetSet($offset, $value)
{
$this->$offset = $value;
}
public function offsetUnset($offset)
{
$this->$offset = null;
}
protected $__children = [];
...