PHPackages                             yii2-extensions/nested-sets-behavior - 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. yii2-extensions/nested-sets-behavior

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

yii2-extensions/nested-sets-behavior
====================================

yii2-extension

0.1.0(10mo ago)3211BSD-3-ClausePHPPHP &gt;=8.1CI passing

Since Jul 9Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/yii2-extensions/nested-sets-behavior)[ Packagist](https://packagist.org/packages/yii2-extensions/nested-sets-behavior)[ GitHub Sponsors](https://github.com/terabytesoftw)[ RSS](/packages/yii2-extensions-nested-sets-behavior/feed)WikiDiscussions main Synced 1mo ago

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

 [ ![Yii Framework](https://camo.githubusercontent.com/cc75562bca6e54e98046e4fb187ef8d96c997a8f31c6f4d2f6ed0c816413b47a/68747470733a2f2f7777772e7969696672616d65776f726b2e636f6d2f696d6167652f7969695f6c6f676f5f6c696768742e737667) ](https://github.com/yii2-extensions/nested-sets-behavior)

Nested sets behavior
====================

[](#nested-sets-behavior)

 [ ![PHP version](https://camo.githubusercontent.com/8c0cc099469e78d1c3ab294bd8b4e29dd7ad459d84ca953058f4034eeb3e0695/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f253345253344382e312d3737374242342e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d706870266c6f676f436f6c6f723d7768697465) ](https://www.php.net/releases/8.1/en.php) [ ![Yii 2.0.x](https://camo.githubusercontent.com/9395c61bf1902514d93587795b76f57f6a87d1e1c4248d1c8b0957e968093889/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f322e302e782d3030373341412e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d796969266c6f676f436f6c6f723d7768697465) ](https://github.com/yiisoft/yii2/tree/2.0.53) [ ![Yii 22.0.x](https://camo.githubusercontent.com/6419cb138fb08a8fcb971e8e780bed42fdb74f683a39b22ca0cd2059f246ce43/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f32322e302e782d3030373341412e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d796969266c6f676f436f6c6f723d7768697465) ](https://github.com/yiisoft/yii2/tree/22.0) [ ![PHPUnit](https://camo.githubusercontent.com/b6f82d984a84d7bf9c66889f0785e0c7010afde3cca6304ec775a9295456ca3e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f796969322d657874656e73696f6e732f6e65737465642d736574732d6265686176696f722f6275696c642e796d6c3f7374796c653d666f722d7468652d6261646765266c6162656c3d504850556e6974) ](https://github.com/yii2-extensions/nested-sets-behavior/actions/workflows/build.yml) [ ![Mutation Testing](https://camo.githubusercontent.com/2b45a9bde126a438204ddd59d1f96d2786e9d2a8871beef4316196089dfbfed3/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666f722d7468652d62616467652675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969322d657874656e73696f6e732532466e65737465642d736574732d6265686176696f722532466d61696e) ](https://dashboard.stryker-mutator.io/reports/github.com/yii2-extensions/nested-sets-behavior/main) [ ![PHPStan](https://camo.githubusercontent.com/9d5bd0e85391a354f212b2064f31a8e5921e50e6648ceb3e5f2d390fa751daf1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f796969322d657874656e73696f6e732f6e65737465642d736574732d6265686176696f722f7374617469632e796d6c3f7374796c653d666f722d7468652d6261646765266c6162656c3d5048505374616e) ](https://github.com/yii2-extensions/nested-sets-behavior/actions/workflows/static.yml)

A powerful behavior for managing hierarchical data structures using the nested sets pattern in Yii ActiveRecord models.

Efficiently store and query tree structures like categories, menus, organizational charts, and any hierarchical data with high-performance database operations.

Features
--------

[](#features)

- ✅ **Efficient Tree Operations** - Insert, move, delete nodes with automatic boundary management.
- ✅ **Flexible Queries** - Find ancestors, descendants, siblings, leaves, and roots.
- ✅ **Multiple Trees Support** - Manage multiple independent trees in the same table.
- ✅ **Query Optimization** - Single-query operations for maximum performance.
- ✅ **Transaction Safety** - All operations are wrapped in database transactions.
- ✅ **Validation &amp; Error Handling** - Comprehensive validation with clear error messages.

Database support
----------------

[](#database-support)

[![Microsoft SQL Server](https://camo.githubusercontent.com/2b646cc1f71ad946bf47bd817fb768ea11e59318ee1c83d55f41676e84f503b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d6963726f736f667425323053514c2532305365727665722d4343323932373f7374796c653d666f722d7468652d6261646765266c6f676f3d6d6963726f736f667425323073716c253230736572766572266c6f676f436f6c6f723d7768697465)](https://github.com/yii2-extensions/nested-sets-behavior/actions/workflows/build-mssql.yml)[![MySQL](https://camo.githubusercontent.com/1b3c1f020909d47c98e2a00b9e5484c252c84684ed3573ffff1e9ee721421887/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d7973716c2d3434373941312e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d6d7973716c266c6f676f436f6c6f723d7768697465)](https://github.com/yii2-extensions/nested-sets-behavior/actions/workflows/build-mysql.yml)[![Oracle](https://camo.githubusercontent.com/57f8644bda1d6def5cd32a4fd75f5a11501cc2d90aea1e752257f0c8d5b80f31/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f7261636c652d4638303030303f7374796c653d666f722d7468652d6261646765266c6f676f3d6f7261636c65266c6f676f436f6c6f723d7768697465)](https://github.com/yii2-extensions/nested-sets-behavior/actions/workflows/build-oracle.yml)[![PostgreSQL](https://camo.githubusercontent.com/c25bd055ad07a1ff82fe93bd35615a9f506469fee758a89409dea6edf545c4ba/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706f737467726573716c2d3431363965313f7374796c653d666f722d7468652d6261646765266c6f676f3d706f737467726573716c266c6f676f436f6c6f723d7768697465)](https://github.com/yii2-extensions/nested-sets-behavior/actions/workflows/build-pgsql.yml)[![SQLite](https://camo.githubusercontent.com/46377b8c7f5401d7c13376c3e7453c6ae1855b86db2e9398d46cc2920557a162/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73716c6974652d3030334235372e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d73716c697465266c6f676f436f6c6f723d7768697465)](https://github.com/yii2-extensions/nested-sets-behavior/actions/workflows/build.yml)

How it works
------------

[](#how-it-works)

The nested sets model is a technique for storing hierarchical data in a relational database. Unlike adjacency lists (parent\_id approach), nested sets enable efficient tree operations with minimal database queries.

1. **Creates root nodes** using the nested sets pattern with `lft`, `rgt`, and `depth` fields.
2. **Manages hierarchy** automatically when inserting, moving, or deleting nodes.
3. **Optimizes queries** using boundary values for efficient tree traversal.
4. **Supports transactions** to ensure data integrity during complex operations.

**Why nested sets?**

- **Fast queries**: Get all descendants with a single query (`lft BETWEEN parent.lft AND parent.rgt`).
- **Efficient tree operations**: No recursive queries needed for tree traversal.
- **Automatic maintenance**: Left/right boundaries are calculated automatically.
- **Depth tracking**: Easy to limit query depth or build breadcrumbs.

```
Example tree structure:
Electronics (1,12,0)
├── Mobile Phones (2,7,1)
│   └── Smartphones (3,6,2)
│       └── iPhone (4,5,3)
└── Computers (8,11,1)
    └── Laptops (9,10,2)

Numbers represent: (left, right, depth)

```

### Installation

[](#installation)

```
composer require yii2-extensions/nested-sets-behavior
```

### Database setup

[](#database-setup)

The package includes ready-to-use migrations for creating the necessary database structure.

#### Quick setup (Recommended)

[](#quick-setup-recommended)

1. **Configure console application**:

```
