PHPackages                             shopware/dbal-nested-set - 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. shopware/dbal-nested-set

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

shopware/dbal-nested-set
========================

A Doctrine DBAL nested set implementation

v1.2.3(5y ago)9425.5k↓37%3MITPHPPHP ^7.2|^8.0

Since Jul 24Pushed 1y ago16 watchersCompare

[ Source](https://github.com/shopware5/dbal-nested-set)[ Packagist](https://packagist.org/packages/shopware/dbal-nested-set)[ RSS](/packages/shopware-dbal-nested-set/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (3)Versions (10)Used By (0)

Doctrine DBAL Nested Set
========================

[](#doctrine-dbal-nested-set)

Used in B2B Suite for Shopware 5
--------------------------------

[](#used-in-b2b-suite-for-shopware-5)

A multi root nested set implementation for DBAL users.

Description
-----------

[](#description)

This library provides you write, read and inspection classes for nested sets with multiple root nodes per table. Solely relying on the Doctrine DBAL.

Contrary to other solutions this library has clear boundaries and leaves the software design up to you.

- No tree abstraction - just nested sets
- No entities - just method calls with plain parameters

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

[](#installation)

Use composer to install the library

```
> composer require shopware/dbal-nested-set
```

Usage
-----

[](#usage)

You always need a configuration that sets up the basic column names of your implementation:

```
use Shopware\DbalNestedSet\NestedSetConfig;

$config = new NestedSetConfig(
    'id', // Primary key column name
    'left', // left column name
    'right',  // right column name
    'level' // level column name
);
```

Then you can use the `NestedSetFactory` to create the different classes of the library.

```
use Shopware\DbalNestedSet\NestedSetFactory;
use Doctrine\DBAL\Connection;

$writer = NestedSetFactory::createWriter($dbalConnection, $config);
```

### Create a tree

[](#create-a-tree)

You may want to create a normalized schema for nested set tables, this can be accomplished through the `NestedSetTableFactory`. It will create the base DDL for a tree with indexes. So if you want to add a simple tree with a name column and an autoincrement id it will look like this:

```
$tableFactory = NestedSetFactory::createTableFactory($connection, $config);

$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $tableFactory->createTable(
    $schema,
    'tree', // table name
    'root_id' // nested set root id
);
$table->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true]);
$table->addColumn('name', 'string', ['length' => 255]);
$table->setPrimaryKey(['id']);

$addSql = $schema->toSql($connection->getDatabasePlatform());
```

Of course this is optional and may be accomplished through any schema configuration tool.

### Modify the tree

[](#modify-the-tree)

The library provides a `NestedSetWriter` class that contains all insert, move and update operations. All operations should be reminiscent of `Doctrine\DBAL\Connection::insert()` and `Doctrine\DBAL\Connection::update()` and just require plain data.

As an example you can use this to create a tree

```
$writer = NestedSetFactory::createWriter($dbalConnection, $config);

// create a Root node
$writer->insertRoot('tree', 'root_id', 100, ['name' => 'Clothing']);

// create subnodes
$writer->insertAsFirstChild('tree', 'root_id', 1, ['name' => 'Men']);
$writer->insertAsNextSibling('tree', 'root_id', 2, ['name' => 'Women']);
$writer->insertAsFirstChild('tree', 'root_id', 2, ['name' => 'Suits']);
$writer->insertAsFirstChild('tree', 'root_id', 3, ['name' => 'Dresses']);
$writer->insertAsNextSibling('tree', 'root_id', 5, ['name' => 'Skirts']);
$writer->insertAsNextSibling('tree', 'root_id', 6, ['name' => 'Blouses']);
$writer->insertAsFirstChild('tree', 'root_id', 4, ['name' => 'Jackets']);
$writer->insertAsFirstChild('tree', 'root_id', 4, ['name' => 'Slacks']);
$writer->insertAsFirstChild('tree', 'root_id', 5, ['name' => 'Evening Gowns']);
$writer->insertAsNextSibling('tree', 'root_id', 10, ['name' => 'Sun Dresses']);
```

And then use the writer to move nodes around

```
$writer->moveAsNextSibling('tree', 'root_id', 4, 7);
```

### Inspect nodes

[](#inspect-nodes)

You may want to retrieve information about different nodes. This can be done through the `NestedSetTableNodeInspector`.

```
$inspector = NestedSetFactory::createTableNodeInspector($connection, $config);

$inspector->isLeaf('tree', 'root_id', 9); // true | false
$inspector->isAncestor('tree', 'root_id', 1, 2) // true | false
```

### Inspect the tree

[](#inspect-the-tree)

The `NestedSetQueryFactory` helps retrieve a set of nodes from the tree. Since the library has no concept of entities it will only prepare query builders for you ready to add selects, joins and other conditions.

```
$queryFactory = NestedSetFactory::createQueryFactory($connection, $config);
$data = $queryFactory
            ->createChildrenQueryBuilder('tree', 't', 'root_id', 2)
            ->select('*')
            ->execute()
            ->fetchAll();
```

Local development
-----------------

[](#local-development)

If you want to develop locally you may have to configure the database access through a little shell script:

```
#!/usr/bin/env bash

export DB_USER='foo'
export DB_PASSWORD='bar'
export DB_HOST='baz'
export DB_NAME='dbal_nested_set'

bin/phpunit
```

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity41

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% 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 ~175 days

Recently: every ~288 days

Total

8

Last Release

1991d ago

PHP version history (2 changes)v1.0PHP ^7.0

v1.2.3PHP ^7.2|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3e33c241de9be12145b80356591da2a7b875f1111d77a8f0a5474f55f02c91c1?d=identicon)[dnoegel](/maintainers/dnoegel)

![](https://www.gravatar.com/avatar/7c45ef9077b73fce78afbfab2fa27e611a453dd77de003e2785ac84105d02bef?d=identicon)[shyim](/maintainers/shyim)

---

Top Contributors

[![JanPietrzyk](https://avatars.githubusercontent.com/u/786075?v=4)](https://github.com/JanPietrzyk "JanPietrzyk (34 commits)")[![teiling88](https://avatars.githubusercontent.com/u/4624237?v=4)](https://github.com/teiling88 "teiling88 (9 commits)")[![mitelg](https://avatars.githubusercontent.com/u/6985627?v=4)](https://github.com/mitelg "mitelg (4 commits)")[![emmer91](https://avatars.githubusercontent.com/u/11973718?v=4)](https://github.com/emmer91 "emmer91 (2 commits)")[![PascalThesing](https://avatars.githubusercontent.com/u/14017436?v=4)](https://github.com/PascalThesing "PascalThesing (2 commits)")

---

Tags

dbalnested-setphp-libraryquery-buildertree-structure

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/shopware-dbal-nested-set/health.svg)

```
[![Health](https://phpackages.com/badges/shopware-dbal-nested-set/health.svg)](https://phpackages.com/packages/shopware-dbal-nested-set)
```

###  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.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

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

Reliese Components for Laravel Framework code generation.

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

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

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