2017 © Pedro Peláez
 

library stefano-tree

Nested Set(MPTT) implementation for PHP

image

stefano/stefano-tree

Nested Set(MPTT) implementation for PHP

  • Monday, July 23, 2018
  • by Stefano123
  • Repository
  • 2 Watchers
  • 16 Stars
  • 3,629 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 3 Open issues
  • 37 Versions
  • 20 % Grown

The README.md

Tree

Latest Stable Version Build Status Coverage Status Scrutinizer Code Quality License Total Downloads Monthly Downloads, (*1)

Buy me a coffee, (*2)

Donate on PayPal, (*3)

Nested Set implementation for PHP., (*4)

Live demo, (*5)

Features

  • NestedSet(MPTT - Modified Pre-order Tree Traversal)
  • Support scopes (multiple independent tree in one db table)
  • Rebuild broken tree
  • Tested with MySQL/MariaDB and PostgreSQL but should work with any database vendor which support transaction
  • Supported PDO, Zend Framework 1, Laminas Db, Doctrine 2 DBAL and Doctrine 3 DBAL. It is easy to implement support for any framework
  • Support nested transaction
  • PHP 7 and PHP 8 support

Dependencies

  • This library has no external dependencies. Can work with pure PHP.

Installation

Run following command in terminal, (*6)

composer require stefano/stefano-tree

Create Tree Adapter

key type required default value note
tableName string yes
idColumnName string yes
leftColumnName string no lft
rightColumnName string no rgt
levelColumnName string no level
parentIdColumnName string no parent_id
sequenceName string see note Required for PostgreSQL
scopeColumnName string see note If empty scope support is disabled
dbSelectBuilder callable no see Join table example below
use \StefanoTree\NestedSet;

$options = array(
    'tableName'    => 'tree_traversal',
    'idColumnName' => 'tree_traversal_id',
    // other options
);

$dbAdapter = pure \PDO, Zend1 Db Adapter, Laminas Db Adapter, Doctrine DBAL Connection or any class which implements StefanoTree\NestedSet\Adapter\AdapterInterface interface 

$tree = new NestedSet($options, $dbAdapter);
  • You can join table.
$options = array(
    'tableName'       => 'tree_traversal',
    'idColumnName'    => 'tree_traversal_id',
    'dbSelectBuilder' => function() {
         // You can use any "callable" like function or object
         // Select must be without where or order part
         return 'SELECT tree_traversal.*, m.something, ...'
           .' FROM tree_traversal'
           .' LEFT JOIN metadata AS m ON tree_traversal.id=m.tree_id';
     }, 
    // other options
);

$tree = new NestedSet($options, $dbAdapter);

API

Creating nodes

  • Create root node
use StefanoTree\Exception\ValidationException;

try {
    $data = array(
        // values
        // id_column_name => uuid 
    );

    // create root node.
    $rootNodeId = $tree->createRootNode($data);

    // create root node. Second param "$scope" is required only if scope support is enabled.
    $rootNodeId = $tree->createRootNode($data, $scope);    
} catch (ValidationException $e) {
    $errorMessage = $e->getMessage();
}    
  • Create new node. You can create new node at 4 different locations.

placements, (*7)

use StefanoTree\Exception\ValidationException;

try {
    $targetNodeId = 10;

    $data = array(
        // values
        // id_column_name => uuid 
    );

    $nodeId = $tree->addNode($targetNodeId, $data, $tree::PLACEMENT_CHILD_TOP);
    $nodeId = $tree->addNode($targetNodeId, $data, $tree::PLACEMENT_CHILD_BOTTOM);
    $nodeId = $tree->addNode($targetNodeId, $data, $tree::PLACEMENT_TOP);
    $nodeId = $tree->addNode($targetNodeId, $data, $tree::PLACEMENT_BOTTOM);
} catch (ValidationException $e) {
    $errorMessage = $e->getMessage();
}    

Update Node

use StefanoTree\Exception\ValidationException;

try {
    $targetNodeId = 10;

    $data = array(
        // values
    );

    $tree->updateNode($targetNodeId, $data);
} catch (ValidationException $e) {
    $errorMessage = $e->getMessage();
}    

Move node

  • You can move node at 4 different locations.

placements, (*8)

use StefanoTree\Exception\ValidationException;

try {
    $sourceNodeId = 15;
    $targetNodeId = 10;

    $tree->moveNode($sourceNodeId, $targetNodeId, $tree::PLACEMENT_CHILD_TOP);
    $tree->moveNode($sourceNodeId, $targetNodeId, $tree::PLACEMENT_CHILD_BOTTOM);
    $tree->moveNode($sourceNodeId, $targetNodeId, $tree::PLACEMENT_TOP);
    $tree->moveNode($sourceNodeId, $targetNodeId, $tree::PLACEMENT_BOTTOM);
} catch (ValidationException $e) {
    $errorMessage = $e->getMessage();
}        

Delete node or branch

use StefanoTree\Exception\ValidationException;

try {
    $nodeId = 15;

    $tree->deleteBranch($nodeId);
} catch (ValidationException $e) {
    $errorMessage = $e->getMessage();
}    

Getting nodes

  • Get descendants
$nodeId = 15;

// all descendants
$tree->getDescendantsQueryBuilder()
     ->get($nodeId);

// all descendants result as nested array
$tree->getDescendantsQueryBuilder()
     ->get($nodeId, true);

// only children     
$tree->getDescendantsQueryBuilder()
     ->excludeFirstNLevel(1)
     ->levelLimit(1)
     ->get($nodeId);

// exclude first level($nodeId) from result
$tree->getDescendants()
     ->excludeFirstNLevel(1)
     ->get($nodeId);

// exclude first two levels from result
$tree->getDescendantsQueryBuilder()
     ->excludeFirstNLevel(2)
     ->get($nodeId);

// return first 4 level
$tree->getDescendantsQueryBuilder()
     ->levelLimit(4)
     ->get($nodeId);

// exclude branch from  result
$tree->getDescendantsQueryBuilder()
     ->excludeBranch(22)
     ->get($nodeId);
  • Get Ancestors
$nodeId = 15;

// get all
$tree->getAncestorsQueryBuilder()
     ->get($nodeId);

// get all as nested array
$tree->getAncestorsQueryBuilder()
     ->get($nodeId, true);

// exclude last node($nodeId) from result
$tree->getAncestorsQueryBuilder()
     ->excludeLastNLevel(1)
     ->get($nodeId);

// exclude first two levels from result
$tree->getAncestorsQueryBuilder()
     ->excludeFirstNLevel(2)
     ->get($nodeId);

Validation and Rebuild broken tree

  • Check if tree is valid
use StefanoTree\Exception\ValidationException;

try {
    $satus = $tree->isValid($rootNodeId);
} catch (ValidationException $e) {
    $errorMessage = $e->getMessage();
}
  • Rebuild broken tree
use StefanoTree\Exception\ValidationException;

try {
    $tree->rebuild($rootNodeId);
} catch (ValidationException $e) {
    $errorMessage = $e->getMessage();
}

Contributing

Any contributions are welcome. If you find any issue don't hesitate to open a new issue or send a pull request., (*9)

Buy me a coffee, (*10)

The Versions

23/07 2018

dev-develop

dev-develop https://github.com/bartko-s/stefano-tree

Nested Set(MPTT) implementation for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine symfony pdo tree doctrine dbal mptt

23/07 2018

dev-master

9999999-dev https://github.com/bartko-s/stefano-tree

Nested Set(MPTT) implementation for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine symfony pdo tree doctrine dbal mptt

23/07 2018

4.0.0

4.0.0.0 https://github.com/bartko-s/stefano-tree

Nested Set(MPTT) implementation for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine symfony pdo tree doctrine dbal mptt

08/07 2018
21/05 2018

3.1.1

3.1.1.0 https://github.com/bartko-s/stefano-tree

Framework agnostic library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine tree mptt

21/05 2018

3.1.0

3.1.0.0 https://github.com/bartko-s/stefano-tree

Framework agnostic library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine tree mptt

15/01 2018

3.0.1

3.0.1.0 https://github.com/bartko-s/stefano-tree

Framework agnostic library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine tree mptt

27/12 2017

3.0.0

3.0.0.0 https://github.com/bartko-s/stefano-tree

Framework agnostic library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine tree mptt

24/04 2017

2.0.2

2.0.2.0 https://github.com/bartko-s/stefano-tree

Framework agnostic library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine tree mptt

17/11 2016

2.0.1

2.0.1.0 https://github.com/bartko-s/stefano-tree

Framework agnostic library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine tree mptt

17/11 2016

2.0.0

2.0.0.0 https://github.com/bartko-s/stefano-tree

Framework agnostic library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

by Štefan Bartko

nested set zend zend framework doctrine tree mptt

10/11 2015
06/11 2015
06/11 2015
05/11 2015
04/09 2015

1.3.5

1.3.5.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

nested set zend doctrine tree mptt

11/03 2015

1.3.4

1.3.4.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

nested set zend doctrine tree mptt

25/07 2014

1.3.3

1.3.3.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

nested set zend doctrine tree mptt

05/06 2014

1.3.2

1.3.2.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

nested set zend doctrine tree mptt

23/05 2014
24/01 2014

1.3.0

1.3.0.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

nested set zend doctrine tree mptt

24/01 2014

1.2.2

1.2.2.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

nested set zend tree mptt

24/01 2014

1.2.1

1.2.1.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

nested set zend tree mptt

14/01 2014

1.2.0

1.2.0.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

nested set zend tree mptt

30/09 2013
26/09 2013

1.1.2

1.1.2.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

tree mptt

05/08 2013

1.1.1

1.1.1.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

tree mptt

30/07 2013

1.1.0

1.1.0.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

tree mptt

04/06 2013

1.0.3

1.0.3.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

tree mptt

03/06 2013

1.0.2

1.0.2.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

tree mptt zend-module

31/05 2013

1.0.1

1.0.1.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

tree mptt zend-module

27/05 2013

1.0.0

1.0.0.0 https://github.com/bartko-s/stefano-tree

Library for managing tree structures

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Štefan Bartko

tree zend-module