2017 © Pedro Peláez
 

library closure-table

Adjacency List’ed Closure Table database design pattern implementation for Laravel

image

espadav8/closure-table

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  • Thursday, July 16, 2015
  • by EspadaV8
  • Repository
  • 2 Watchers
  • 2 Stars
  • 1,794 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 71 Forks
  • 1 Open issues
  • 30 Versions
  • 0 % Grown

The README.md

ClosureTable

Build Status Latest Stable Version Total Downloads, (*1)

NOTE: Master branch is now for Laravel 5.
If you use Laravel 4, please see L4 branch!

Hi, this is a database package for Laravel. It's intended to use when you need to operate hierarchical data in database. The package is an implementation of a well-known database design pattern called Closure Table. The package includes generators for models and migrations., (*2)

Installation

To install the package, put the following in your composer.json:, (*3)

"require": {
    "espadav8/closure-table": "4.*"
}

And to app/config/app.php:, (*4)

'providers' => array(
        // ...
        EspadaV8\ClosureTable\ClosureTableServiceProvider::class,
    ),

Setup your ClosureTable

Create models and migrations

For example, let's assume you're working on pages. You can just use an artisan command to create models and migrations automatically without preparing all the stuff by hand. Open terminal and put the following:, (*5)

php artisan closuretable:make --entity=page

All options of the command:
1. --namespace, -ns [optional]: namespace for classes, set by --entity and --closure options, helps to avoid namespace duplication in those options
2. --entity, -e: entity class name; if namespaced name is used, then the default closure class name will be prepended with that namespace
3. --entity-table, -et [optional]: entity table name
4. --closure, -c [optional]: closure class name
5. --closure-table [optional], -ct: closure table name
6. --models-path, -mdl [optional]: custom models path
7. --migrations-path, -mgr [optional]: custom migrations path
8. --use-innodb and -i [optional]: InnoDB migrations have been made optional as well with new paramaters. Setting this will enable the InnoDB engine., (*6)

That's almost all, folks! The ‘dummy’ stuff has just been created for you. You will need to add some fields to your entity migration because the created ‘dummy’ includes just required id, parent_id, position, and real depth columns:
, (*7)

  1. id is a regular autoincremented column
  2. parent_id column is used to simplify immediate ancestor querying and, for example, to simplify building the whole tree
  3. position column is used widely by the package to make entities sortable
  4. real depth column is also used to simplify queries and reduce their number

By default, entity’s closure table includes the following columns:
1. Autoincremented identifier
2. Ancestor column points on a parent node
3. Descendant column points on a child node
4. Depth column shows a node depth in the tree, (*8)

It is by closure table pattern design, so remember that you must not delete these four columns., (*9)

Remember that many things are made customizable, so see ‘Customization’ for more information., (*10)

Time of coding

Once your models and their database tables are created, at last, you can start actually coding. Here I will show you ClosureTable's specific approaches., (*11)

Direct ancestor (parent)

$parent = Page::find(15)->getParent();

Ancestors

$page = Page::find(15);
$ancestors = $page->getAncestors();
$ancestors = $page->getAncestorsTree(); // Tree structure
$ancestors = $page->getAncestorsWhere('position', '=', 1);
$hasAncestors = $page->hasAncestors();
$ancestorsNumber = $page->countAncestors();

Direct descendants (children)

$page = Page::find(15);
$children = $page->getChildren();
$hasChildren = $page->hasChildren();
$childrenNumber = $page->countChildren();

$newChild = new Page(array(
    'title' => 'The title',
    'excerpt' => 'The excerpt',
    'content' => 'The content of a child'
));

$newChild2 = new Page(array(
    'title' => 'The title',
    'excerpt' => 'The excerpt',
    'content' => 'The content of a child'
));

$page->addChild($newChild);

//you can set child position
$page->addChild($newChild, 5);

//you can get the child
$child = $page->addChild($newChild, null, true);

$page->addChildren([$newChild, $newChild2]);

$page->getChildAt(5);
$page->getFirstChild();
$page->getLastChild();
$page->getChildrenRange(0, 2);

$page->removeChild(0);
$page->removeChild(0, true); //force delete
$page->removeChildren(0, 3);
$page->removeChildren(0, 3, true); //force delete

Descendants

$page = Page::find(15);
$descendants = $page->getDescendants();
$descendants = $page->getDescendantsWhere('position', '=', 1);
$descendantsTree = $page->getDescendantsTree();
$hasDescendants = $page->hasDescendants();
$descendantsNumber = $page->countDescendants();

Siblings

$page  = Page::find(15);
$first = $page->getFirstSibling(); //or $page->getSiblingAt(0);
$last  = $page->getLastSibling();
$atpos = $page->getSiblingAt(5);

$prevOne = $page->getPrevSibling();
$prevAll = $page->getPrevSiblings();
$hasPrevs = $page->hasPrevSiblings();
$prevsNumber = $page->countPrevSiblings();

$nextOne = $page->getNextSibling();
$nextAll = $page->getNextSiblings();
$hasNext = $page->hasNextSiblings();
$nextNumber = $page->countNextSiblings();

//in both directions
$hasSiblings = $page->hasSiblings();
$siblingsNumber = $page->countSiblings();

$sibligns = $page->getSiblingsRange(0, 2);

$page->addSibling(new Page);
$page->addSibling(new Page, 3); //third position

//add and get the sibling
$sibling = $page->addSibling(new Page, null, true);

$page->addSiblings([new Page, new Page]);
$page->addSiblings([new Page, new Page], 5); //insert from fifth position

Roots (entities that have no ancestors)

$roots = Page::getRoots();
$isRoot = Page::find(23)->isRoot();
Page::find(11)->makeRoot(0); //at the moment we always have to set a position when making node a root

Entire tree

$tree = Page::getTree();
$treeByCondition = Page::getTreeWhere('position', '>=', 1);

You deal with the collection, thus you can control its items as you usually do. Descendants? They are already loaded., (*12)

$tree = Page::getTree();
$page = $tree->find(15);
$children = $page->getChildren();
$child = $page->getChildAt(3);
$grandchildren = $page->getChildAt(3)->getChildren(); //and so on

Moving

$page = Page::find(25);
$page->moveTo(0, Page::find(14));
$page->moveTo(0, 14);

Deleting subtree

If you don't use foreign keys for some reason, you can delete subtree manually. This will delete the page and all its descendants:, (*13)

$page = Page::find(34);
$page->deleteSubtree();
$page->deleteSubtree(true); //with subtree ancestor
$page->deleteSubtree(false, true); //without subtree ancestor and force delete

Customization

You can customize default things in your own classes created by the ClosureTable artisan command:
1. Entity table name: change protected $table property
2. Closure table name: do the same in your ClosureTable (e.g. PageClosure)
3. Entity's parent_id, position, and real depth column names: change return values of getParentIdColumn(), getPositionColumn(), and getRealDepthColumn() respectively
4. Closure table's ancestor, descendant, and depth columns names: change return values of getAncestorColumn(), getDescendantColumn(), and getDepthColumn() respectively., (*14)

The Versions

16/07 2015

dev-develop

dev-develop

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

16/07 2015

dev-master

9999999-dev

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

16/07 2015

v5.1.4

5.1.4.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

16/07 2015

v5.1.3

5.1.3.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

13/07 2015

v5.1.2

5.1.2.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

06/07 2015

v5.1.1

5.1.1.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

06/07 2015

v5.1.0

5.1.0.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

03/07 2015

v5.0.1

5.0.1.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

03/07 2015

dev-feature/v6

dev-feature/v6

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

03/07 2015

v5.0.0

5.0.0.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

13/03 2015

dev-feature/traits

dev-feature/traits

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

09/03 2015

v4.0.1

4.0.1.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

09/03 2015

dev-feature/dump-autloader

dev-feature/dump-autloader

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

09/03 2015

v4

4.0.0.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

09/03 2015

dev-feature/postgresql-ctid

dev-feature/postgresql-ctid

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

09/03 2015

dev-feature/laravel-5

dev-feature/laravel-5

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

14/02 2015

v3.1.1

3.1.1.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

03/02 2015

v3.1

3.1.0.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

05/01 2015

v3.0.5

3.0.5.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

29/06 2014

3.x-dev

3.9999999.9999999.9999999-dev

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

18/05 2014

v3.0.4

3.0.4.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

27/03 2014

v3.0.3

3.0.3.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

26/03 2014

v3.0.2

3.0.2.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

13/03 2014

v3.0.1

3.0.1.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

12/03 2014

v3.0

3.0.0.0

Adjacency List’ed Closure Table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

database laravel pages closure table categories hierarchy adjacency list

11/10 2013

2.x-dev

2.9999999.9999999.9999999-dev

Closure table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel

05/10 2013

v2.1.6

2.1.6.0

Closure table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel

04/10 2013

v2.1.5

2.1.5.0

Closure table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel

28/09 2013

v2.1

2.1.0.0

Closure table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel

23/09 2013

v2.0

2.0.0.0

Closure table database design pattern implementation for Laravel

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel