PHPackages                             vakata/phptree - 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. [Database &amp; ORM](/categories/database)
4. /
5. vakata/phptree

ActiveLibrary[Database &amp; ORM](/categories/database)

vakata/phptree
==============

A collection of PHP classes for storing a tree in a relational database

3.2.4(1y ago)26.2k↓69.2%1MITPHPCI failing

Since May 10Pushed 1y ago2 watchersCompare

[ Source](https://github.com/vakata/phptree)[ Packagist](https://packagist.org/packages/vakata/phptree)[ Docs](https://github.com/vakata/phptree)[ RSS](/packages/vakata-phptree/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (25)Used By (0)

phptree
=======

[](#phptree)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5c91c75b6c4e51768a280da00f118886433b217a999b895af7688e66b23b4fd3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f76616b6174612f706870747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/vakata/phptree)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/c6a8768020217f756da97337fbb4ffb7ba5ee591c84348cc06d0c775222753e0/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f76616b6174612f706870747265652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/vakata/phptree)[![Code Climate](https://camo.githubusercontent.com/89bc4b3cd11048225160e80d896da6176bb69e5f4456f9dc8e8eb0e057c0df32/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6769746875622f76616b6174612f706870747265652e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/vakata/phptree)[![Tests Coverage](https://camo.githubusercontent.com/7192dcb59968cdb009612678183f9688527e89319eac217246f05cb03d720433/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f636f7665726167652f6769746875622f76616b6174612f706870747265652e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/vakata/phptree)

Storing trees in a relational database. Keep in mind the tree needs to have a single root, so it is probably safe to begin with this structure (this example is mySQL, but it should be clear):

```
CREATE TABLE struct (
    id  int(10) unsigned NOT NULL AUTO_INCREMENT,
    lft int(10) unsigned NOT NULL,
    rgt int(10) unsigned NOT NULL,
    lvl int(10) unsigned NOT NULL,
    pid int(10) unsigned NOT NULL,
    pos int(10) unsigned NOT NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO struct VALUES (1, 1, 2, 0, 0, 0);

# now you can use 1 as your tree root
```

Install
-------

[](#install)

Via Composer

```
$ composer require vakata/phptree
```

Usage
-----

[](#usage)

```
// create an instance
$dbc = new \vakata\database\DB("mysqli://root@127.0.0.1/treedb");
$tree = new \vakata\phptree\Tree(
    $dbc,
    'tree_table',
    [ 'id' => 'id', 'parent' => 'pid', 'position' => 'pos', 'level' => 'lvl', 'left' => 'lft', 'right' => 'rgt' ]
);

// WORKING WITH NODES
$tree->getRoot()->getChildren(); // get all children of the root

$tree->getRoot()->addChild(new \vakata\phptree\Node(['key' => 'val1'])); // create a node
$tree->getRoot()->addChild(new \vakata\phptree\Node(['key' => 'val2'])); // create a node
$tree->save();
$tree->getNode(2)->moveTo($tree->getRoot(), 2);
$tree->getNode(3)->copyTo($tree->getRoot());
$tree->getNode(3)->remove();
```

Read more in the [API docs](docs/README.md)

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [vakata](https://github.com/vakata)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~138 days

Recently: every ~77 days

Total

24

Last Release

517d ago

Major Versions

1.0.2 → 2.0.02016-12-02

2.1.0 → 3.0.02020-04-09

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/146052?v=4)[Ivan Bozhanov](/maintainers/vakata)[@vakata](https://github.com/vakata)

---

Top Contributors

[![vakata](https://avatars.githubusercontent.com/u/146052?v=4)](https://github.com/vakata "vakata (40 commits)")

---

Tags

treevakata

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/vakata-phptree/health.svg)

```
[![Health](https://phpackages.com/badges/vakata-phptree/health.svg)](https://phpackages.com/packages/vakata-phptree)
```

###  Alternatives

[gedmo/doctrine-extensions

Doctrine behavioral extensions

4.1k124.6M430](/packages/gedmo-doctrine-extensions)[stof/doctrine-extensions-bundle

Integration of the gedmo/doctrine-extensions with Symfony

2.0k89.6M457](/packages/stof-doctrine-extensions-bundle)[knplabs/doctrine-behaviors

Doctrine Behavior Traits

92313.0M68](/packages/knplabs-doctrine-behaviors)[typicms/nestablecollection

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

88337.7k26](/packages/typicms-nestablecollection)[nevadskiy/laravel-tree

Tree-like structure for Eloquent models.

67109.3k10](/packages/nevadskiy-laravel-tree)[umbrellio/laravel-ltree

Extension LTree (Postgres) for Laravel

34120.1k](/packages/umbrellio-laravel-ltree)

PHPackages © 2026

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