2017 © Pedro Peláez
 

library nested_set

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

image

ghopper/nested_set

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  • Saturday, April 22, 2017
  • by GHopper
  • Repository
  • 1 Watchers
  • 0 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

nested_set

Tiny class which provides basic functions for manipulating with "Nested Set" database tree., (*1)

Preparing

Create the database table, (*2)

DROP TABLE IF EXISTS ns_tree;
CREATE TABLE ns_tree (
    id INT AUTO_INCREMENT PRIMARY KEY,
    lft INT NOT NULL,
    rgt INT NOT NULL,
    -- you can add whatever you want with text type
    -- such as 'name','description','link' etc
    name VARCHAR(255),
    link VARCHAR(255)
);
INSERT INTO ns_tree (lft,rgt,name,link) VALUES(1,2,'root','root_link');

Setup PDO-connection. The class sets PDO attribute PDO::ATTR_ERRMODE equal to PDO::ERRMODE_EXCEPTION if it isn't set yet, (*3)

...
$pdo = new \PDO(DB_CONN_STRING, DB_USER, DB_PASS,
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);

Configuring

$aConfig = array(
    'tb_name' => 'ns_tree',
    'tb_field_index'=> 'id',
    'tb_field_left' => 'lft',
    'tb_field_right' => 'rgt',
    'tb_extra_fields'=> array(
        'name',
        'link'
    )
);

Usage

create the object

$ns = new CNestedSet($pdo, $aConfig);

add new node

Pass array with extra fields' values, (*4)

$aNewNode = array(
    'name' => 'new node name',
    'link' => 'new node link'
);
$nd1 = $ns->addChild(1, $aNewNode); 

Returns array with 'depth' values., (*5)

print_r($ns->getTree($nd1), TRUE);

Look at the examples folder for more information., (*6)

Available methods

  • getTree($parent_id) - return all descendants of $parent_id node
  • addChild($parent_id, $values) - add a new node to $parent_id which contains $values
  • addRootChild($values) - add new node to the root element
  • moveTree($cur_parent_id, $new_parent_id) - move the whole tree to a new parent
  • deleteTree($node_id) - delete $node_id with all descendants
  • deleteNode($node_id) - delete $node_id only and shift the descendants to level up

Requirements

  • database has to transactions support
  • database has to locks support

The Versions

22/04 2017

dev-master

9999999-dev

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

26/02 2017

v0.3.5

0.3.5.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

26/02 2017

v0.3.6

0.3.6.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

26/02 2017

v0.3.4

0.3.4.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

25/02 2017

v0.3.3

0.3.3.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

25/02 2017

v0.3.2

0.3.2.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

24/02 2017

v0.3.1

0.3.1.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

23/02 2017

v0.2

0.2.0.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

23/02 2017

v0.3

0.3.0.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set

22/02 2017

v0.1

0.1.0.0

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

  Sources   Download

MIT

The Development Requires

by Nikolay Poryadin

nested set