bupy7 / doctrine-nested-set
Doctrine ORM 的嵌套集
3.0.0
2021-04-13 11:35 UTC
Requires
- php: ^7.1 || ^8.0
- doctrine/orm: ^2.6
Requires (Dev)
- doctrine/data-fixtures: ^1.3.3
- friendsofphp/php-cs-fixer: ^2.14.0
- php-coveralls/php-coveralls: ^2.1.0
- phpunit/phpunit: ^7.5.2 || ^9.5.4
README
Doctrine ORM 的库,用于发布嵌套集模型树。
特性
- 多个根节点。
- 获取所有项目。
- 获取子项目。
- 获取子代项目。
- 获取父项目。
- 获取根项目。
- 获取祖先项目。
- 作为某个项目的最后一个添加。
- 作为某个项目第一个添加。
- 待办:在某个项目后添加。
- 待办:在某个项目前添加。
- 作为第一个根项目添加。
- 作为最后一个根项目添加。
- 移除一个项目。
- 待办:在某个项目后移动项目。
- 待办:在某个项目前移动项目。
- 待办:设置为根项目。
- 待办:设置为某个父项目的子项目。
安装
安装此扩展的首选方式是通过 composer。
$ composer require bupy7/doctrine-nested-set "*"
配置
添加实体
use Bupy7\Doctrine\NestedSet\NestedSetInterface; /** * @Entity(repositoryClass="CategoryRepository") */ class Category implements NestedSetInterface { /** * @Id * @Column(type="integer") * @GeneratedValue * @var int|null */ private $id; /** * @Column(type="integer", name="root_key") * @var int */ private $rootKey = 1; /** * @Column(type="integer") * @var int */ private $level = 1; /** * @Column(type="integer", name="left_key") * @var int */ private $leftKey; /** * @Column(type="integer", name="right_key") * @var int */ private $rightKey; /** * @Column(type="string") * @var string */ private $name; public function getId(): ?int { return $this->id; } public function setId($id): NestedSetInterface { $this->id = $id; return $this; } public function getRootKey(): int { return $this->rootKey; } public function setRootKey(int $rootKey): NestedSetInterface { $this->rootKey = $rootKey; return $this; } public function getLevel(): int { return $this->level; } public function setLevel(int $level): NestedSetInterface { $this->level = $level; return $this; } public function getLeftKey(): int { return $this->leftKey; } public function setLeftKey(int $leftKey): NestedSetInterface { $this->leftKey = $leftKey; return $this; } public function getRightKey(): int { return $this->rightKey; } public function setRightKey(int $rightKey): NestedSetInterface { $this->rightKey = $rightKey; return $this; } public function getName(): string { return $this->name; } public function setName(string $name): Category { $this->name = $name; return $this; } }
添加仓库
use Bupy7\Doctrine\NestedSet\NestedSetRepositoryAbstract; class CategoryRepository extends NestedSetRepositoryAbstract { }
用法
树示例
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- - CPU
- Laptops
- Tablets
获取所有项目
$categories = $categoryRepository->findAll(); // var_dump($categories); // // - PC // - Motherboards // - RAM // - DDR3 // - DDR4 // - CPU // - Laptops // - Tablets
获取子项目
$parentCategory = $categoryRepository->findOneByName('RAM'); $children = $categoryRepository->findChildren($parentCategory); // var_dump($children); // // - DDR3 // - DDR4
获取子代项目
$parentCategory = $categoryRepository->findOneByName('PC'); $descendants = $categoryRepository->findDescendants($parentCategory); // var_dump($children); // // - Motherboards // - RAM // - DDR3 // - DDR4 // - CPU
获取父项目
$childrenCategory = $categoryRepository->findOneByName('RAM'); $parent = $categoryRepository->findOneParent($childrenCategory); // var_dump($parent); // // - PC
获取根项目
$roots = $categoryRepository->findRoots(); // var_dump($parent); // // - PC // - Laptops // - Tablets
获取祖先项目
$childrenCategory = $categoryRepository->findOneByName('DDR3'); $roots = $categoryRepository->findAncestors($childrenCategory); // var_dump($parent); // // - RAM // - PC
作为某个项目的第一个添加
$category = new Category(); $category->setName('DDR2'); $parentCategory = $categoryRepository->findOneByName('RAM'); $categoryRepository->prepend($category, $parentCategory); $entityManager->clear(); // optional, if you need
树结果
- PC
- - Motherboards
- - RAM
- - - DDR2
- - - DDR3
- - - DDR4
- - CPU
- Laptops
- Tablets
作为某个项目的最后一个添加
$category = new Category(); $category->setName('LGA 1151v2'); $parentCategory = $categoryRepository->findOneByName('CPU'); $categoryRepository->append($category, $parentCategory); $entityManager->clear(); // optional, if you need
树结果
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- - CPU
- - - LGA 1151v2
- Laptops
- Tablets
作为第一个根项目添加
$category = new Category(); $category->setName('Phones'); $categoryRepository->prepend($category); $entityManager->clear(); // optional, if you need
树结果
- Phones
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- - CPU
- Laptops
- Tablets
作为最后一个根项目添加
$category = new Category(); $category->setName('Phones'); $categoryRepository->append($category); $entityManager->clear(); // optional, if you need
树结果
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- - CPU
- Laptops
- Tablets
- Phones
移除一个项目
$category = $categoryRepository->findOneByName('CPU'); $categoryRepository->remove($category); $entityManager->clear(); // optional, if you need
树结果
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- Laptops
- Tablets
或移除及其子代
$category = $categoryRepository->findOneByName('PC'); $categoryRepository->remove($category); $entityManager->clear(); // optional, if you need
树结果
- Laptops
- Tablets
需要手动处理时,您可以使用事务
use Doctrine\DBAL\TransactionIsolationLevel; // if you want to change isolation level // $oldIsolationLevel = $entityManager->getConnection()->getTransactionIsolation(); // $entityManager->getConnection()->setTransactionIsolation(TransactionIsolationLevel::SERIALIZABLE); $entityManager->beginTransaction(); try { $category = $categoryRepository->findOneByName('PC'); $categoryRepository->remove($category); $entityManager->commit(); $entityManager->clear(); // optional, if you need } catch (Exception $e) { $entityManager->rollback(); throw $e; } finally { // if you changed isolation level // $entityManager->getConnection()->setTransactionIsolation($oldIsolationLevel); }
许可证
doctrine-nested-set 根据 BSD 3-Clause 许可证发布。