PHPackages                             boardfy/mptt-multi-independent-tree - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. boardfy/mptt-multi-independent-tree

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

boardfy/mptt-multi-independent-tree
===================================

A PHP library providing a multi independent tree implementation of the modified preorder tree traversal algorithm

3.1(7y ago)1206LGPL-3.0PHPPHP &gt;=5.0.0

Since Sep 14Pushed 7y ago2 watchersCompare

[ Source](https://github.com/Boardfy/Mptt_MultiIndependentTree)[ Packagist](https://packagist.org/packages/boardfy/mptt-multi-independent-tree)[ Docs](https://github.com/Boardfy/Mptt_MultiIndependentTree)[ RSS](/packages/boardfy-mptt-multi-independent-tree/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)DependenciesVersions (11)Used By (0)

![]()

Mptt\_MultiTree
===============

[](#mptt_multitree)

*A PHP library providing an implementation of the modified preorder tree traversal algorithm with support for multiindependent trees*

What is Modified Preorder Tree Traversal
----------------------------------------

[](#what-is-modified-preorder-tree-traversal)

MPTT is a fast algorithm for storing hierarchical data (like categories and their subcategories) in a relational database. This is a problem that most of us have had to deal with, and for which we've used an [adjacency list](http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/), where each item in the table contains a pointer to its parent and where performance will naturally degrade with each level added as more queries need to be run in order to fetch a subtree of records.

The aim of the modified preorder tree traversal algorithm is to make retrieval operations very efficient. With it you can fetch an arbitrary subtree from the database using just two queries. The first one is for fetching details for the root node of the subtree, while the second one is for fetching all the children and grandchildren of the root node.

The tradeoff for this efficiency is that updating, deleting and inserting records is more expensive, as there's some extra work required to keep the tree structure in a good state at all times. Also, the modified preorder tree traversal approach is less intuitive than the adjacency list approach because of its algorithmic flavour.

For more information about the modified preorder tree traversal method, read this excellent article called [Storing Hierarchical Data in a Database](http://blogs.sitepoint.com/hierarchical-data-database-2/).

What is Mptt\_MultiIndependentTree
----------------------------------

[](#what-is-mptt_multiindependenttree)

**Mptt\_MultiIndependentTree** is a PHP library, derived from stefangabos' [Zebra\_Mptt](https://github.com/stefangabos/Zebra_Mptt), that provides an implementation of the modified preorder tree traversal algorithm making it easy to implement the MPTT algorithm in your PHP applications.

It provides multi independent tree methods for adding nodes anywhere in a tree, deleting nodes, moving and copying nodes around a tree and methods for retrieving various information about the nodes.

Mptt\_Multiindependenttree uses [table locks](http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html) making sure that database integrity is always preserved and that concurrent MySQL sessions don't compromise data integrity. Also, Mptt\_MultiIndependentTree uses a caching mechanism which has as result the fact that regardless of the type, or the number of retrieval operations, **the database is read only once per script execution!**

Features
--------

[](#features)

- Allow to use the same table to store independent trees, achieving faster operations on independent trees structures(ie: tree per client).
- provides methods for adding nodes anywhere in a tree, deleting nodes, moving and copying nodes around a tree and methods for retrieving various information about the nodes
- uses a caching mechanism which has as result the fact that regardless of the type, or the number of retrieval operations, **the database is read only once per script execution**
- uses [transactions](https://dev.mysql.com/doc/refman/5.6/en/commit.html) making sure that database integrity is always preserved and that concurrent MySQL sessions don't compromise data integrity
- uses **mysqli** extension
- code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to [E\_ALL](https://web.archive.org/web/20160226192832/http://www.php.net/manual/en/function.error-reporting.php)

Requirements
------------

[](#requirements)

PHP 5.0.0+, MySQL 4.1.22+

Installation
------------

[](#installation)

Download the latest version, unpack it, and load it in your project

```
require_once 'Mptt_MultiIndependentTree.php';
```

Installation with Composer
--------------------------

[](#installation-with-composer)

You can install Mptt\_MultiIndependentTree via [Composer](https://packagist.org/packages/boardfy/mptt-multi-independent-tree)

```
# get the latest stable release
composer require boardfy/Mptt_MultiIndependentTree

# get the latest commit
composer require boardfy/Mptt_MultiIndependentTree:dev-master
```

Install MySQL table
-------------------

[](#install-mysql-table)

Notice a directory called *install* containing a file named *mptt.sql*. This file contains the SQL code that will create the table used by the class to store its data. Import or execute the SQL code using your preferred MySQL manager (like phpMyAdmin or the fantastic Adminer) into a database of your choice.

How to use
----------

[](#how-to-use)

```
// include the Mptt_MultiIndependentTree class
require 'path/to/Mptt_MultiIndependentTree.php';

// instantiate a new object
$mptt = new Mptt_MultiIndependentTree();

// populate the table

// add 'Food' as a topmost node
$food = $mptt->add(0, 'Food');

// 'Fruit' and 'Meat' are direct descendants of 'Food'
$fruit = $mptt->add($food, 'Fruit');
$meat = $mptt->add($food, 'Meat');

// 'Red' and 'Yellow' are direct descendants of 'Fruit'
$red = $mptt->add($fruit, 'Red');
$yellow = $mptt->add($fruit, 'Yellow');

// add a fruit of each color
$cherry = $mptt->add($red, 'Cherry');
$banana = $mptt->add($yellow, 'Banana');

// add two kinds of meat
$mptt->add($meat, 'Beef');
$mptt->add($meat, 'Pork');

// move 'Banana' to 'Meat'
$mptt->move($banana, $meat);

// get a flat array of descendants of 'Meat'
$mptt->get_children($meat);

// get a multidimensional array (a tree) of all the data in the database
$mptt->get_tree();
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 90.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~200 days

Recently: every ~117 days

Total

10

Last Release

2821d ago

Major Versions

2.3.4 → 3.0.02017-11-30

PHP version history (3 changes)2.2.4PHP &gt;=4.4.9

2.3.0PHP &gt;=5.0.5

2.3.1PHP &gt;=5.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/93071f7071edfe6b214c798e1868683033d88c9279e3022bce32cb48137afd7a?d=identicon)[boardfy](/maintainers/boardfy)

---

Top Contributors

[![stefangabos](https://avatars.githubusercontent.com/u/741165?v=4)](https://github.com/stefangabos "stefangabos (59 commits)")[![Redominus](https://avatars.githubusercontent.com/u/22024214?v=4)](https://github.com/Redominus "Redominus (5 commits)")[![erlingormar](https://avatars.githubusercontent.com/u/738922?v=4)](https://github.com/erlingormar "erlingormar (1 commits)")

---

Tags

phptreemultiMPTTtraversalmodifiedpreorder

### Embed Badge

![Health badge](/badges/boardfy-mptt-multi-independent-tree/health.svg)

```
[![Health](https://phpackages.com/badges/boardfy-mptt-multi-independent-tree/health.svg)](https://phpackages.com/packages/boardfy-mptt-multi-independent-tree)
```

###  Alternatives

[loophp/phptree

An implementation of tree data structure

981.8M2](/packages/loophp-phptree)[efureev/laravel-trees

Multi-Tree structures for Laravel

14253.3k4](/packages/efureev-laravel-trees)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.2

222.5M3](/packages/chdemko-sorted-collections)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
