PHPackages                             kappa/doctrine-mptt - 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. kappa/doctrine-mptt

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

kappa/doctrine-mptt
===================

Modified Pre-order Tree Traversal doctrine implementation

0660PHP

Since Mar 3Pushed 11y ago1 watchersCompare

[ Source](https://github.com/Kappa-org/DoctrineMPTT)[ Packagist](https://packagist.org/packages/kappa/doctrine-mptt)[ RSS](/packages/kappa-doctrine-mptt/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Kappa\\DoctrineMPTT
===================

[](#kappadoctrinemptt)

[![Build Status](https://camo.githubusercontent.com/57fdeb5ee6fd78d319629afe12e7426552179b68f0842815cb23ceab00b53537/68747470733a2f2f7472617669732d63692e6f72672f4b617070612d6f72672f446f637472696e654d5054542e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Kappa-org/DoctrineMPTT)

Modified Pre-order Tree Traversal doctrine implementation.

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

[](#requirements)

Full list of dependencies you can get from [Composer config file](https://github.com/Kappa-org/DoctrineMPTT/blob/master/composer.json)

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

[](#installation)

The best way to install Kappa\\DoctrineMPTT is using [Composer](https://getcomposer.org)

```
$ composer require kappa/doctrine-mptt:@dev
```

and register extension

```
extensions:
    doctrine: Kappa\Doctrine\DI\DoctrineExtension
    doctrineMPTT: Kappa\DoctrineMPTT\DI\DoctrineMPTTExtension
```

Benefits
--------

[](#benefits)

1. Minimize count of queries. For delete is needed 3 queries (1x `DELETE`, 2x `UPDATE`), for create is needed 3 queries (1x `INSERT`, 2x `UPDATE`) and for move item is needed **only 1x `UPDATE`query**. This algorithm is much faster and more fuel efficient than other commonly used methods, such as classical parent - children method.
2. Full control over the nesting depth or count of branches

Restrictions and warnings
-------------------------

[](#restrictions-and-warnings)

1. This package was be tested on MySQL, SQLite and PostgreSQL and it is compatible with them
2. Because this package is working directly with database to minimize count of queries after use `moveItem()` or `insertItem()` you should refresh loaded entities. **Attention! you must save all updates before call this methods to avoids conflicts**

Configuration
-------------

[](#configuration)

You can set custom names for used columns:

```
doctrineMPTT:
        entityClass: Your\entity
		originalLeftName: _lft
		leftColumnName: lft
		rightColumnName: rgt
		depthColumnName: depth
```

Configuration may not be in config file but can be set `Kappa\DoctrineMPTT\TraversableManager::setConfigurator`.

Usages
------

[](#usages)

Package provide main `Kappa\DoctrineMPTT\TraversableManager` which can be used for all manipulations. All operations are performed over entity which is instance of `Kappa\DoctrineMPTT\Entities\TraversableInterface`.

### Entity

[](#entity)

You can use own entity but, your entity must implement `Kappa\DoctrineMPTT\Entities\TraversableInterface` interface. For easier implementation you can use `Kappa\DoctrineMPTT\Entities\Traversable` trait which implements all requires methods and columns.

### Manager

[](#manager)

Original tree structure

[![Original](./docs/images/original.png)](./docs/images/original.png)

`Kappa\DoctrineMPTT\TraversableManager` provides three methods by which we can do all operations.

#### Insert item

[](#insert-item)

`insertItem(TraversableInterface $actual, TraversableInterface $parent = null, $refresh)`

Second argument is new parent and actual item (first argument) will be included under this parent item. Last argument is bool and if is set to `true` entities will be refreshed. For example: This code generate next tree

```
$parent = $this->repository->find(4);
$actual = new TraversableEntity();
// ....
$this->traversableManager->insertItem($parent, $actual);
```

[![After insert](./docs/images/insertItem.png)](./docs/images/insertItem.png)

If is parent null, actual item will be inserted as last child of root element if it exist. If not exist root element actual item will be inserted as root.

#### Move item

[](#move-item)

`moveItem(TraversableInterface $actual, TraversableInterface $related, action, refresh)`

With this method you can move each item into new place (as predecessor or descendant). Last argument is bool and if is set to `true` entities will be refreshed. For example **predecessor**

```
// (1)
$actual = $this->repository->find(3);
$related = $this->repository->find(2);
$this->traversableManager->moveItem($actual, $related, TraversableManager::PREDECESSOR); // (1) - move actual before related

// (2)
$actual = $this->repository->find(3);
$related = $this->repository->find(4);
$this->traversableManager->moveItem($actual, $related, TraversableManager::DESCENDANT); // (2) - move actual as child of related

// (3)
$actual = $this->repository->find(3);
$this->traversableManager->moveItem($actual, null, TraversableManager::DESCENDANT); // (3) - move actual as last child of root
```

**(1) Result**

[![After move predecessor](./docs/images/moveItemPredecessor.png)](./docs/images/moveItemPredecessor.png)

**(2) Result**

[![After move predecessor](./docs/images/moveItemDescendant.png)](./docs/images/moveItemDescendant.png)

**(3) Result**

[![After move predecessor](./docs/images/moveItemDescendantNull.png)](./docs/images/moveItemDescendantNull.png)

#### Remove item

[](#remove-item)

`removeItem(TraversableInterface $actual)`

Remove item and all its children For example:

```
$actual = $this->repository->find(2);
$this->traversableManager->removeItem($actual);
```

[![After delete](./docs/images/removeItem.png)](./docs/images/removeItem.png)

### Queries

[](#queries)

1. `Kappa\DoctrineMPTT\Queries\Objects\Selectors\GetAll` - returns all items sorted for scalable listing
2. `Kappa\DoctrineMPTT\Queries\Objects\Selectors\GetParents` - returns all parents for actual item
3. `Kappa\DoctrineMPTT\Queries\Objects\Selectors\GetChildren` - returns all children for actual item
4. `Kappa\DoctrineMPTT\Queries\Objects\Selectors\GetPrevious` - return previous item
5. `Kappa\DoctrineMPTT\Queries\Objects\Selectors\GetNext` - return next item
6. `Kappa\DoctrineMPTT\Queries\Objects\Selectors\GetParent` - returns parent item
7. `Kappa\DoctrineMPTT\Queries\Objects\Selectors\GetRoot` - returns tree root

You can use `Kappa\DoctrineMPTT\Queries\SelectorsCollector` as easier getter of query objects

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0c0840abca14f130b93d02cfa8412e99dc0645ca3f7e57cf75cfcb487efe33aa?d=identicon)[Budry](/maintainers/Budry)

---

Top Contributors

[![Budry](https://avatars.githubusercontent.com/u/990676?v=4)](https://github.com/Budry "Budry (105 commits)")

### Embed Badge

![Health badge](/badges/kappa-doctrine-mptt/health.svg)

```
[![Health](https://phpackages.com/badges/kappa-doctrine-mptt/health.svg)](https://phpackages.com/packages/kappa-doctrine-mptt)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M545](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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