PHPackages                             nineinchnick/closure-table-manager - 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. nineinchnick/closure-table-manager

ActiveLibrary

nineinchnick/closure-table-manager
==================================

PHP library that helps maintain adjacency list SQL structures.

10752[2 issues](https://github.com/nineinchnick/closure-table-manager/issues)PHP

Since Jul 8Pushed 11y ago2 watchersCompare

[ Source](https://github.com/nineinchnick/closure-table-manager)[ Packagist](https://packagist.org/packages/nineinchnick/closure-table-manager)[ RSS](/packages/nineinchnick-closure-table-manager/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

closure-table-manager
=====================

[](#closure-table-manager)

PHP library that helps maintain adjacency list SQL structures.

TL;DR: It allows fetching all ancestors/descendants (indirect parents/children) in a single query, without using recursive queries.

Inspired by:

- [SQL::Tree Perl module](https://github.com/mlawren/sqltree)
-

Currently supported databases:

- PostgreSQL
- SQLite 3
- MySQL and MariaDB

Pull requests with other databases support are very welcome.

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

[](#installation)

Using composer:

```
curl -sS https://getcomposer.org/installer | php
./composer.phar require nineinchnick/closure-table-manager:dev-master
```

Usage
-----

[](#usage)

Call `Manager::getQueries()` to get an array of SQL queries that create a helper table to store ancestor/descendant relationships from the main table and triggers that maintain it.

When installed, triggers will block the following operations:

- Changing the primary key value
- Creating loops

A command line script is provided:

```
Usage: ./vendor/bin/closureTable.php [options] [operands]
Options:
  -d, --dsn          DSN connection string or just the driver name (pgsql, sqlite, mysql).
  -t, --table        Table name.
  -p, --parent       Parent foreign key column name.
  -i, --pk           Primary key column name.
  --pk_type           Primary key and parent column type.
  --path             Path column name; if set, additional triggers will be generated.
  --path_from        Column which value will be used to build a path. Its values cant't contain path_separator.
  --path_separator   Path separator character.
  --table_suffix     Suffix of the closure table.

```

Example
-------

[](#example)

Having the following tables:

```
CREATE TABLE products (
  id INTEGER,
  category_id INTEGER NOT NULL REFERENCES categories (id),
  -- ...
  PRIMARY KEY(id)
);

CREATE TABLE categories (
  id INTEGER,
  parent_id INTEGER NOT NULL REFERENCES categories (id),
  -- ...
  PRIMARY KEY(id)
);
```

It is quite common to ask database for all products in given category and it's subcategories.

```
    SELECT p.*
      FROM products p
INNER JOIN categories_tree c on p.category_id = c.id
     WHERE c.parent_id = ;
```

When user is *in* some category, we would like to show him *path* to this category. So he could easily move to some parent category.

```
    SELECT c.*
      FROM categories c
INNER JOIN categories_tree t on c.id = t.parent_id
     WHERE c.id = 4
  ORDER BY t.depth DESC;
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

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/2e98951e0acfcdd5b86ad8e5fb23f501b4259ccbb107ef1669c4049c0f2485cc?d=identicon)[nineinchnick](/maintainers/nineinchnick)

---

Top Contributors

[![nineinchnick](https://avatars.githubusercontent.com/u/795177?v=4)](https://github.com/nineinchnick "nineinchnick (7 commits)")

### Embed Badge

![Health badge](/badges/nineinchnick-closure-table-manager/health.svg)

```
[![Health](https://phpackages.com/badges/nineinchnick-closure-table-manager/health.svg)](https://phpackages.com/packages/nineinchnick-closure-table-manager)
```

PHPackages © 2026

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