cbsi/doctrine2-nestedset

此Doctrine2扩展实现了nested set模型(修改后的先序遍历算法)以供Doctrine2使用。这允许将层次化数据,每个项目都有一个父项和零个或多个子项的数据集合,存储在关系数据库的扁平表中。

dev-master 2013-09-19 20:56 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:39:36 UTC


README

此Doctrine2扩展实现了nested set模型(修改后的先序遍历算法)以供Doctrine2使用。这允许将层次化数据,一个集合中的每个项目都有一个父项和零个或多个子项,存储在关系数据库的扁平表中。有关nested set模型的更多信息,请参阅

https://dev.mysqlserver.cn/tech-resources/articles/hierarchical-data.html

简介

Nested Set是存储层次化数据的一种解决方案,它提供了非常快速的读取访问。然而,更新nested set树的成本更高。因此,此解决方案最适合那些读得比写得多得多的层次结构。由于网络的性质,大多数Web应用程序都是如此。

设置

要将您的模型设置为Nested Set,您的实体类必须实现DoctrineExtensions\NestedSet\Node接口。每个实体类必须包含映射字段以保存Nested Set的左右值。

以下是一个使用注解映射的示例

namespace Entity;

use DoctrineExtensions\NestedSet\Node;

/**
 * @Entity
 */
class Category implements Node
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    private $id;

    /**
     * @Column(type="integer")
     */
    private $lft;

    /**
     * @Column(type="integer")
     */
    private $rgt;

    /**
     * @Column(type="string", length="16")
     */
    private $name;


    public function getId() { return $this->id; }

    public function getLeftValue() { return $this->lft; }
    public function setLeftValue($lft) { $this->lft = $lft; }

    public function getRightValue() { return $this->rgt; }
    public function setRightValue($rgt) { $this->rgt = $rgt; }

    public function getName() { return $this->name; }
    public function setName($name) { $this->name = $name; }

    public function __toString() { return $this->name; }
}

通常,您不需要,也不应该与左右字段交互。这些字段用于内部管理树结构。

多棵树

Nested Set实现可以配置为允许您的表具有多个根节点,因此同一表中可以存在多棵树。这是通过实现DoctrineExtensions\NestedSet\MultipleRootNode接口(而不是DoctrineExtensions\NestedSet\Node)并映射根字段来完成的。

扩展我们的注解示例

/**
 * @Column(type="integer")
 */
private $root;

public function getRootValue() { return $this->root; }
public function setRootValue($root) { $this->root = $root; }

像左右字段一样,您通常不需要与根值交互。

与树一起工作

在您成功将模型设置为nested set之后,您就可以开始使用它了。与Doctrine2的nested set实现一起工作主要涉及两个类:Manager和NodeWrapper。NodeWrapper封装了您的实体类,使您能够访问底层的树结构。Manager提供了创建新树和检索现有树的方法。

要从数据库中检索整个树

$config = new Config($em, 'Entity\Category');
$nsm = new Manager($config);
$rootNode = $nsm->fetchTree(1);

在此示例中,$rootNode是封装了您的模型根节点的NodeWrapper实例。要访问您的模型对象

$modelObject = $rootNode->getNode();

创建根节点

$config = new Config($em, 'Entity\Category');
$nsm = new Manager($config);

$category = new Category();
$category->setName('Root Category 1');

$rootNode = $nsm->createRoot($category);

插入节点

$child1 = new Category();
$child1->setName('Child Category 1');

$child2 = new Category();
$child2->setName('Child Category 2');

$rootNode->addChild($child1);
$rootNode->addChild($child2);

删除节点

您必须始终使用NodeWrapper::delete()方法而不是EntityManager的delete方法来删除节点。NodeWrapper::delete()负责在删除节点时更新树

$category = $em->getRepository('Entity\Category')->findOneByName('Child Category 1');
$node = $nsm->wrapNode($category);
$node->delete();

删除节点也将删除该节点的所有后代。因此,在您删除节点之前,请确保将它们移动到其他位置,如果您不希望删除它们。

移动节点

移动节点很简单。NodeWrapper提供了一些方法来在树之间移动节点

  • moveAsLastChildOf($other)
  • moveAsFirstChildOf($other)
  • moveAsPrevSiblingOf($other)
  • moveAsNextSiblingOf($other)

检查节点

您可以使用以下一些函数检查节点及其类型

$isLeaf = $node->isLeaf();
$isRoot = $node->isRoot();

检查和检索兄弟节点

您可以使用以下方法轻松检查节点是否有任何下一个或上一个兄弟节点

$hasNextSib = $node->hasNextSibling();
$hasPrevSib = $node->hasPrevSibling();

如果存在,您也可以使用以下方法检索下一个或上一个兄弟节点

$nextSib = $node->getNextSibling();
$prevSib = $node->getPrevSibling();

如果您想检索所有兄弟元素的数组,可以使用getSiblings()方法。

$siblings = $node->getSiblings();

检查和检索后代

您可以使用以下方法检查一个节点是否有父节点或子节点。

$hasChildren = $node->hasChildren();
$hasParent = $node->hasParent();

您可以使用以下方法检索节点的第一个和最后一个子节点。

$firstChild = $node->getFirstChild();
$lastChild = $node->getLastChild();

或者如果您想检索节点的父节点

$parent = $node->getParent();

您可以使用以下方法获取节点的子节点。

$children = $node->getChildren();

getChildren()方法只返回直接后代。如果您想获取所有后代,请使用getDescendants()方法。

您可以使用以下方法获取节点的后代或祖先。

$descendants = $node->getDescendants();
$ancestors = $node->getAncestors();

有时您可能只想获取子节点或后代的数量。您可以使用以下方法来完成此操作。

$numChildren = $node->getNumberChildren();
$numDescendants = $node->getNumberDescendants();

getDescendants()方法接受一个参数,您可以使用该参数指定结果的分支深度。例如,getDescendants(1)只检索直接后代(位于下一级,等同于getChildren())。

渲染简单的树

$tree = $nsm->fetchTreeAsArray(1);

foreach ($tree as $node) {
    echo str_repeat('&nbsp;&nbsp;', $node->getLevel()) . $node . "<br>";
}

高级用法

前几节解释了Doctrine嵌套集实现的简单用法。本节将进一步说明。

带关系的树检索

如果您是要求严格的软件开发人员,这个问题可能已经出现在您的脑海中:“我如何检索带有相关数据的树/分支?”简单示例:您想显示一个类别树,但您还希望显示每个类别的相关数据,比如该类别中最热销产品的详细信息。如前几节所示检索树,并在遍历树时简单地访问关系是可能的,但会产生大量的不必要的数据库查询。幸运的是,Manager和一些嵌套集实现的灵活性可以帮助您解决问题。嵌套集实现使用QueryBuilder对象进行所有数据库操作。通过给您提供访问嵌套集实现的基查询构建器的权限,您可以在使用嵌套集的同时释放QueryBuilder的全部功能。

$qb = $em->createQueryBuilder();
$qb->select('c.name, p.name, m.name')
    ->from('Category', 'c')
    ->leftJoin('c.HottestProduct', 'p')
    ->leftJoin('p.Manufacturer', 'm');

现在我们需要将上述查询设置为树的基查询

$nsm->getConfiguration()->setBaseQueryBuilder($qb);
$tree = $nsm->fetchTree(1);

这就是您需要的所有相关数据的树,都在一个查询中。

如果您没有设置自己的基查询,则会自动为您内部创建一个。

完成操作后,将基查询重置为正常是一个好主意。

$nsm->getConfiguration()->resetBaseQueryBuilder();

事务

当使用NodeWrapper中的方法修改树时,每个方法都会立即执行。这与使用正常Doctrine2实体进行操作不同,在操作中,更改是通过EntityManager排队,并在调用flush时执行。

如果您正在执行多个更改,建议将这些更改包装在事务中。

$em->getConnection()->beginTransaction();
try {

    $root = $nsm->createRoot(new Category('Root'));
    $root->addChild(new Category('Child 1'));
    $root->addChild(new Category('Child 2'));

    $em->getConnection()->commit();
} catch (Exception $e) {
    $em->close();
    $em->getConnection()->rollback();
    throw $e;
}

自定义左、右和根字段

NestedSet要求您在实体类中包含左、右和根字段。默认情况下,NestedSet期望这些字段分别命名为lft、rgt和root。您可以通过管理器配置自定义这些字段的名称。

$config = new Config($em, 'Entity\Category');
$config->setLeftFieldName('nsLeft');
$config->setRightFieldName('nsRight');
$config->setRootFieldName('nsRoot');
$nsm = new Manager($config);

结论

NestedSet使在Doctrine2中管理层次数据变得快速简单。