inlm / closure-table
LeanMapper 中闭包表的特性
v1.1.0
2017-12-27 14:34 UTC
Requires
- php: >=5.4.0
- tharos/leanmapper: ^2.2 || ^3.0
This package is auto-updated.
Last update: 2024-09-17 10:10:38 UTC
README
LeanMapper 闭包表特性
安装
composer require inlm/closure-table
库需要 PHP 5.4.0 或更高版本。
示例
数据库表
CREATE TABLE `category` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) COLLATE utf8mb4_czech_ci NOT NULL, `parent_id` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `parent_id` (`parent_id`), CONSTRAINT `category_ibfk_2` FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`) ) ENGINE=InnoDB; CREATE TABLE `category_closure` ( `ancestor_id` int(10) unsigned NOT NULL, `descendant_id` int(10) unsigned NOT NULL, `depth` int(10) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`ancestor_id`,`descendant_id`), KEY `descendant_id` (`descendant_id`), CONSTRAINT `category_closure_ibfk_1` FOREIGN KEY (`ancestor_id`) REFERENCES `category` (`id`), CONSTRAINT `category_closure_ibfk_2` FOREIGN KEY (`descendant_id`) REFERENCES `category` (`id`) ) ENGINE=InnoDB;
仓库
<?php class CategoryRepository extends \LeanMapper\Repository { use \Inlm\ClosureTable\TClosureTableRepository; }
实体
<?php /** * @property-read int $id * @property string $name * @property Category|NULL $parent m:hasOne(parent_id) * @property-read Category[] $parents m:hasMany(descendant_id:category_closure:ancestor_id:category) [optional] */ class Category extends \LeanMapper\Entity { use \Inlm\ClosureTable\TClosureTableEntity; /** * Returns direct children, ordered by 'name' * @return Category[] */ public function getChildren() { return $this->getChildrenEntities(array('name')); } }
实体 API
$category->getChildren(); // returns direct children (children.parent_id = category.id) $category->getParents(); // returns parent entities ordered by `depth` (from root) $category->getDescendants(); // returns all descendants $category->getAncestors(); // returns all ancestors $category->getDepth(); // returns entity depth in collection, for standalone entity returns 0
许可证: 新BSD许可证
作者: Jan Pecha, https://www.janpecha.cz/