Package Data | |
---|---|
Maintainer Username: | EspadaV8 |
Maintainer Contact: | iwanow.jan@gmail.com (Jan Iwanow) |
Package Create Date: | 2015-03-06 |
Package Last Update: | 2015-07-16 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-17 03:03:04 |
Package Statistics | |
---|---|
Total Downloads: | 1,818 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 3 |
Total Forks: | 1 |
Total Open Issues: | 0 |
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.
To install the package, put the following in your composer.json:
"require": {
"espadav8/closure-table": "4.*"
}
And to app/config/app.php
:
'providers' => array(
// ...
EspadaV8\ClosureTable\ClosureTableServiceProvider::class,
),
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:
php artisan closuretable:make --entity=page
All options of the command:
--namespace
, -ns
[optional]: namespace for classes, set by --entity
and --closure
options, helps to avoid namespace duplication in those options--entity
, -e
: entity class name; if namespaced name is used, then the default closure class name will be prepended with that namespace--entity-table
, -et
[optional]: entity table name--closure
, -c
[optional]: closure class name--closure-table
[optional], -ct
: closure table name--models-path
, -mdl
[optional]: custom models path--migrations-path
, -mgr
[optional]: custom migrations path--use-innodb
and -i
[optional]: InnoDB migrations have been made optional as well with new paramaters. Setting this will enable the InnoDB engine.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:
id
is a regular autoincremented columnparent_id
column is used to simplify immediate ancestor querying and, for example, to simplify building the whole treeposition
column is used widely by the package to make entities sortablereal depth
column is also used to simplify queries and reduce their numberBy default, entity’s closure table includes the following columns:
It is by closure table pattern design, so remember that you must not delete these four columns.
Remember that many things are made customizable, so see ‘Customization’ for more information.
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.
$parent = Page::find(15)->getParent();
$page = Page::find(15);
$ancestors = $page->getAncestors();
$ancestors = $page->getAncestorsTree(); // Tree structure
$ancestors = $page->getAncestorsWhere('position', '=', 1);
$hasAncestors = $page->hasAncestors();
$ancestorsNumber = $page->countAncestors();
$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
$page = Page::find(15);
$descendants = $page->getDescendants();
$descendants = $page->getDescendantsWhere('position', '=', 1);
$descendantsTree = $page->getDescendantsTree();
$hasDescendants = $page->hasDescendants();
$descendantsNumber = $page->countDescendants();
$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 = 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
$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.
$tree = Page::getTree();
$page = $tree->find(15);
$children = $page->getChildren();
$child = $page->getChildAt(3);
$grandchildren = $page->getChildAt(3)->getChildren(); //and so on
$page = Page::find(25);
$page->moveTo(0, Page::find(14));
$page->moveTo(0, 14);
If you don't use foreign keys for some reason, you can delete subtree manually. This will delete the page and all its descendants:
$page = Page::find(34);
$page->deleteSubtree();
$page->deleteSubtree(true); //with subtree ancestor
$page->deleteSubtree(false, true); //without subtree ancestor and force delete
You can customize default things in your own classes created by the ClosureTable artisan
command:
protected $table
propertyClosureTable
(e.g. PageClosure
)parent_id
, position
, and real depth
column names: change return values of getParentIdColumn()
, getPositionColumn()
, and getRealDepthColumn()
respectivelyancestor
, descendant
, and depth
columns names: change return values of getAncestorColumn()
, getDescendantColumn()
, and getDepthColumn()
respectively.