rundiz/nested-set

用于CRUD嵌套集合数据的类库,例如分类树。

1.0.2 2021-03-03 06:21 UTC

This package is auto-updated.

Last update: 2024-09-10 11:17:55 UTC


README

PHP嵌套集合模型,用于创建/读取/更新/删除树形数据结构(层次结构)。

Latest Stable Version License Total Downloads

Nested Set

一个帮助你查询嵌套集合复杂数据的类。

示例

安装

我建议你通过Composer安装这个库,并使用Composer自动加载来轻松包含文件。如果你不使用Composer,你必须手动包含文件。
请确保文件路径正确。

include_once '/path/to/Rundiz/src/NestedSet.php';

如果你想确保或查看类的工作方式,请按照以下步骤进行,否则请跳过此步骤。
tests/common/test-database-structure.sql文件导入数据库。

连接到数据库

此类使用PDO类。请连接到PDO并将\PDO对象发送到类构造函数。

$db['dsn'] = 'mysql:dbname=YOUR_DB_NAME;host=localhost;port=3306;charset=UTF8';
$db['username'] = 'admin';
$db['password'] = 'pass';
$db['options'] = [
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION // throws PDOException.
];
$PDO = new \PDO($dbConfig['dsn'], $dbConfig['username'], $dbConfig['password'], $dbConfig['options']);
$NestedSet = new \Rundiz\NestedSet\NestedSet($PDO);
$NestedSet->tableName = 'test_taxonomy';// this should be your table name but you can use this for testing.

插入数据

在插入之前,你可以使用getNewPosition()方法获取该分类级别的新的位置。

$new_position = $NestedSet->getNewPosition(4);// result is 4.

每次插入数据时,你必须运行rebuild()方法以生成级别、左、右数据。错误的级别、左、右数据可能导致不正确的列表。

$stmt = $PDO->prepare('INSERT INTO `test_taxonomy`(`parent_id`, `name`, `position`) VALUES (?, ?, ?)');
$stmt->execute([0, 'Root 4', 4]);
$NestedSet->rebuild();

更新数据

要更新数据,你不需要新的位置,但你必须每次都运行rebuild()方法。

$stmt = $PDO->prepare('UPDATE `test_taxonomy`SET `name` = ?, `position` = ? WHERE `id` = ?;
$stmt->execute(['Root 4 new name', 4, 21]);
$NestedSet->rebuild();

检查子项下的父项

如果你想更改所选项目的父项,你可以首先检查所选项目的父项是否位于所选项目的子项之下。
你可以使用isParentUnderMyChildren()方法来检查这一点,而false表示正确的父项(新父项不是编辑项的子项)。
请使用demo-data.sql文件中的数据继续操作。

$editing_item_id = 9;
$new_parent_id = 7;
var_dump($NestedSet->isParentUnderMyChildren($editing_item_id, $new_parent_id));// false (CORRECT! the new parent is not child of this item)

$new_parent_id = 14;
var_dump($NestedSet->isParentUnderMyChildren($editing_item_id, $new_parent_id));// true (INCORRECT! the new parent is child of this item)

读取数据

要读取所选项目及其子项的数据,可以使用getTaxonomyWithChildren()方法。

$options['filter_taxonomy_id'] = 3;// The selected item ID.
$list_txn = $NestedSet->getTaxonomyWithChildren($options);
unset($options);
print_r($list_txn);

要读取所选项目及其父项的数据,直到根项,可以使用getTaxonomyWithParents()方法。

$options['filter_taxonomy_id'] = 13;// The selected item ID.
$list_txn = $NestedSet->getTaxonomyWithParents($options);
unset($options);
print_r($list_txn);

列出项目

你可以使用listTaxonomy()方法列出嵌套数组数据,或使用listTaxonomyFlatten()列出平面数据。

$options = [];
$options['unlimited'] = true;
$list_txn = $NestedSet->listTaxonomy($options);
unset($options);
// The variable $list_txn is array and have 2 keys (total, items).

两种方法的参数相同。

删除项目

你可以选择如何删除项目。

  1. 删除所选项目及其所有子项。
  2. 删除所选项目并将其子项拉到当前父项。

每次删除时,你必须运行rebuild()方法以正确地生成级别、左、右数据。

删除所选项目及其所有子项。

$NestedSet->deleteWithChildren(16);
$NestedSet->rebuild();

删除所选项目并将其子项拉到当前父项。

$NestedSet->deletePullUpChildren(9);
$NestedSet->rebuild();

有关包括自1.x版本以来新增的复杂条件的更多示例,请参阅tests/phpunit文件夹或.wiki/apidoc文件夹中的API文档。