PHPackages                             dryas/f3\_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. dryas/f3\_tree

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

dryas/f3\_tree
==============

A Tree helper for the PHP Fat-Free framework (F3) implementing the modified preorder tree traversal algorithm

v1.0.0(5y ago)0182LGPL-3.0PHPPHP &gt;=5.0.0

Since Apr 15Pushed 5y ago1 watchersCompare

[ Source](https://github.com/dryas/f3_tree)[ Packagist](https://packagist.org/packages/dryas/f3_tree)[ Docs](https://github.com/dryas/f3-tree)[ RSS](/packages/dryas-f3-tree/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

F3\_Tree
========

[](#f3_tree)

*A Tree helper for the PHP Fat-Free framework (F3) implementing the modified preorder tree traversal algorithm*

[![Latest Stable Version](https://camo.githubusercontent.com/c8cb402224080e0bbf748f68f04b5db2a751f6ec677372782f360645122fbf26/68747470733a2f2f706f7365722e707567782e6f72672f64727961732f66335f747265652f762f737461626c65)](https://packagist.org/packages/dryas/f3_tree) [![Total Downloads](https://camo.githubusercontent.com/62a393ced3c0654a871718575769d7b87cfb93ee64314b298a33b2f9158eab93/68747470733a2f2f706f7365722e707567782e6f72672f64727961732f66335f747265652f646f776e6c6f616473)](https://packagist.org/packages/dryas/f3_tree) [![License](https://camo.githubusercontent.com/22ec59755bb038323c13ef0c6982691d0b8dca86d19f5ab8bcd22f7575deed2a/68747470733a2f2f706f7365722e707567782e6f72672f64727961732f66335f747265652f6c6963656e7365)](https://packagist.org/packages/dryas/f3_tree)

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 F3\_Tree
----------------

[](#what-is-f3_tree)

**F3\_Tree** is a tree helper plugin for the [Fat-Free Framework](http://github.com/bcosca/fatfree) that provides an implementation of the modified preorder tree traversal algorithm making it easy to implement the MPTT algorithm in your applications.

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

F3\_Tree is based on the fantastic [Zebra\_Mptt](https://github.com/stefangabos/Zebra_Mptt) library by [Stefan Gabos](https://github.com/stefangabos) and 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, F3\_Tree 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!**

Instead of using the Mysqli extension for database access like the original Zebra\_Mptt library is using, the F3\_Tree library uses the PDO extension like it is used in the Fat-Free Framework.

Features
--------

[](#features)

- provides methods for adding nodes anywhere in the tree, deleting nodes, moving and copying nodes around the 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 [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
- 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+, Fat-Free Framework 3.7.3+

Attention: As PDO offers connectors to different database systems, it is likely that the library also works with other database systems than MySQL (which I'm mostly using). I'm always glad if you want to offer your support on making the library more compatible with different database systems by doing a pull request with your changes. Thanks!

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

[](#installation)

You can install F3\_Tree via [Composer](https://packagist.org/packages/dryas/f3_tree)

```
# get the latest stable release
composer require dryas/f3_tree

# get the latest commit
composer require dryas/f3_tree:dev-master
```

Or you can install it manually by downloading the latest version, unpacking it, and then including it in your project

```
require_once 'path/to/f3_tree.php';
```

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

[](#install-mysql-table)

Notice a directory called *install* containing a file named *f3\_tree.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) into a database of your choice.

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

[](#how-to-use)

```
// If you don't use composers autoloader feature, you need to include the F3_Tree class
require 'path/to/f3_tree.php';

// Instantiate a new object of the class with an object of the Fat-Free Framework DB/SQL
// class as the parameter:
$f3t = new F3_Tree($f3->get('DB'));

// populate the table

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

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

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

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

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

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

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

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

Documentation
-------------

[](#documentation)

You can find more detailed information in the F3\_Tree wiki:

[Documentation](https://github.com/dryas/f3_tree/wiki)

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 86% 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

Unknown

Total

1

Last Release

1905d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/94088?v=4)[Benjamin Kaiser](/maintainers/dryas)[@dryas](https://github.com/dryas)

---

Top Contributors

[![stefangabos](https://avatars.githubusercontent.com/u/741165?v=4)](https://github.com/stefangabos "stefangabos (80 commits)")[![dryas](https://avatars.githubusercontent.com/u/94088?v=4)](https://github.com/dryas "dryas (11 commits)")[![dominikfiala](https://avatars.githubusercontent.com/u/3163672?v=4)](https://github.com/dominikfiala "dominikfiala (1 commits)")[![erlingormar](https://avatars.githubusercontent.com/u/738922?v=4)](https://github.com/erlingormar "erlingormar (1 commits)")

---

Tags

fat-free-frameworkfat-free-pluginsmpttphppreordertraversaltreephptreefat-free frameworkF3MPTTtraversalfat-free-pluginmodifiedpreorderf3\_tree

### Embed Badge

![Health badge](/badges/dryas-f3-tree/health.svg)

```
[![Health](https://phpackages.com/badges/dryas-f3-tree/health.svg)](https://phpackages.com/packages/dryas-f3-tree)
```

###  Alternatives

[loophp/phptree

An implementation of tree data structure

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

Multi-Tree structures for Laravel

14256.3k6](/packages/efureev-laravel-trees)[xfra35/f3-cron

Job scheduling for the PHP Fat-Free Framework

73110.5k](/packages/xfra35-f3-cron)[ikkez/f3-events

Sweet event system for the PHP Fat-Free Framework

2923.3k3](/packages/ikkez-f3-events)[ikkez/f3-pagination

Provides a pagebrowser and some pagination utilities for the PHP Fat-Free Framework

1625.4k2](/packages/ikkez-f3-pagination)

PHPackages © 2026

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