bupy7/doctrine-nested-set
最新稳定版本:3.0.0
Composer 安装命令:
composer require bupy7/doctrine-nested-set
包简介
Nested sets for Doctrine ORM
README 文档
README
Library for Doctrine ORM releasing the nested set model tree.
Features
- Multiple roots.
- Fetching all items.
- Fething children items.
- Fething descendant items.
- Fething a parent item.
- Fething root items.
- Fething ancestor items.
- Adding as last of some item.
- Adding as first of some item.
- TODO: Adding after some item.
- TODO: Adding before some item.
- Adding as a first root item.
- Adding as a last root item.
- Removing a item.
- TODO: Moving item after some item.
- TODO: Moving item before some item.
- TODO: Making as root item.
- TODO: Making as a child item of some parent item.
Install
The preferred way to install this extension is through composer.
$ composer require bupy7/doctrine-nested-set "*"
Configuration
Add entity
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; } }
Add repository
use Bupy7\Doctrine\NestedSet\NestedSetRepositoryAbstract; class CategoryRepository extends NestedSetRepositoryAbstract { }
Usage
Tree example:
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- - CPU
- Laptops
- Tablets
Fetching all items
$categories = $categoryRepository->findAll(); // var_dump($categories); // // - PC // - Motherboards // - RAM // - DDR3 // - DDR4 // - CPU // - Laptops // - Tablets
Fething children items
$parentCategory = $categoryRepository->findOneByName('RAM'); $children = $categoryRepository->findChildren($parentCategory); // var_dump($children); // // - DDR3 // - DDR4
Fething descendant items
$parentCategory = $categoryRepository->findOneByName('PC'); $descendants = $categoryRepository->findDescendants($parentCategory); // var_dump($children); // // - Motherboards // - RAM // - DDR3 // - DDR4 // - CPU
Fething a parent item
$childrenCategory = $categoryRepository->findOneByName('RAM'); $parent = $categoryRepository->findOneParent($childrenCategory); // var_dump($parent); // // - PC
Fething root items
$roots = $categoryRepository->findRoots(); // var_dump($parent); // // - PC // - Laptops // - Tablets
Fething ancestor items
$childrenCategory = $categoryRepository->findOneByName('DDR3'); $roots = $categoryRepository->findAncestors($childrenCategory); // var_dump($parent); // // - RAM // - PC
Adding as first of some item
$category = new Category(); $category->setName('DDR2'); $parentCategory = $categoryRepository->findOneByName('RAM'); $categoryRepository->prepend($category, $parentCategory); $entityManager->clear(); // optional, if you need
Result of tree:
- PC
- - Motherboards
- - RAM
- - - DDR2
- - - DDR3
- - - DDR4
- - CPU
- Laptops
- Tablets
Adding as last of some item
$category = new Category(); $category->setName('LGA 1151v2'); $parentCategory = $categoryRepository->findOneByName('CPU'); $categoryRepository->append($category, $parentCategory); $entityManager->clear(); // optional, if you need
Result of tree:
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- - CPU
- - - LGA 1151v2
- Laptops
- Tablets
Adding as a first root item
$category = new Category(); $category->setName('Phones'); $categoryRepository->prepend($category); $entityManager->clear(); // optional, if you need
Result of tree:
- Phones
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- - CPU
- Laptops
- Tablets
Adding as a last root item
$category = new Category(); $category->setName('Phones'); $categoryRepository->append($category); $entityManager->clear(); // optional, if you need
Result of tree:
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- - CPU
- Laptops
- Tablets
- Phones
Removing a item
$category = $categoryRepository->findOneByName('CPU'); $categoryRepository->remove($category); $entityManager->clear(); // optional, if you need
Result of tree:
- PC
- - Motherboards
- - RAM
- - - DDR3
- - - DDR4
- Laptops
- Tablets
or remove with descendants:
$category = $categoryRepository->findOneByName('PC'); $categoryRepository->remove($category); $entityManager->clear(); // optional, if you need
Result of tree:
- Laptops
- Tablets
You may use transactions when it needs manually
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); }
License
doctrine-nested-set is released under the BSD 3-Clause License.
bupy7/doctrine-nested-set 适用场景与选型建议
bupy7/doctrine-nested-set 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.11k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 06 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「php」 「tree」 「db」 「doctrine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bupy7/doctrine-nested-set 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bupy7/doctrine-nested-set 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bupy7/doctrine-nested-set 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 4.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-06-04