Download the PHP package byteasc/closure-table without Composer
On this page you can find all versions of the php package byteasc/closure-table. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download byteasc/closure-table
More information about byteasc/closure-table
Files in byteasc/closure-table
Package closure-table
Short Description Adjacency List’ed Closure Table database design pattern implementation for Laravel. Includes restore of tree
License MIT
Informations about the package closure-table
ClosureTable
Branches
- L4 supports Laravel 4
- L5.1 supports Laravel < 5.2
- L5.3 supports Laravel 5.2-5.3
- L5.4 supports Laravel 5.4
- master is for any actual Laravel version, so be careful
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.
Installation
To install the package, put the following in your composer.json:
And to app/config/app.php
:
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:
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 number
By default, entity’s closure table includes the following columns:
- Autoincremented identifier
- Ancestor column points on a parent node
- Descendant column points on a child node
- Depth column shows a node depth in the tree
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.
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.
Direct ancestor (parent)
Ancestors
Direct descendants (children)
Descendants
Siblings
Roots (entities that have no ancestors)
Entire tree
You deal with the collection, thus you can control its items as you usually do. Descendants? They are already loaded.
Moving
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:
Customization
You can customize default things in your own classes created by the ClosureTable artisan
command:
- Entity table name: change
protected $table
property - Closure table name: do the same in your
ClosureTable
(e.g.PageClosure
) - Entity's
parent_id
,position
, andreal depth
column names: change return values ofgetParentIdColumn()
,getPositionColumn()
, andgetRealDepthColumn()
respectively - Closure table's
ancestor
,descendant
, anddepth
columns names: change return values ofgetAncestorColumn()
,getDescendantColumn()
, andgetDepthColumn()
respectively.