{"id":19453429,"url":"https://github.com/shopware5/dbal-nested-set","last_synced_at":"2025-04-25T04:30:52.050Z","repository":{"id":55960818,"uuid":"97203328","full_name":"shopware5/dbal-nested-set","owner":"shopware5","description":"A Doctrine DBAL only implementation of multi root nested sets","archived":false,"fork":false,"pushed_at":"2024-11-07T10:02:34.000Z","size":573,"stargazers_count":9,"open_issues_count":0,"forks_count":4,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-03T16:38:49.702Z","etag":null,"topics":["dbal","nested-set","php-library","query-builder","tree-structure"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shopware5.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-14T06:57:25.000Z","updated_at":"2024-11-07T10:02:35.000Z","dependencies_parsed_at":"2023-02-17T19:45:26.117Z","dependency_job_id":null,"html_url":"https://github.com/shopware5/dbal-nested-set","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shopware5%2Fdbal-nested-set","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shopware5%2Fdbal-nested-set/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shopware5%2Fdbal-nested-set/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shopware5%2Fdbal-nested-set/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shopware5","download_url":"https://codeload.github.com/shopware5/dbal-nested-set/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250754590,"owners_count":21481841,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dbal","nested-set","php-library","query-builder","tree-structure"],"created_at":"2024-11-10T17:04:27.476Z","updated_at":"2025-04-25T04:30:48.705Z","avatar_url":"https://github.com/shopware5.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Doctrine DBAL Nested Set\n====================\n\n## Used in B2B Suite for Shopware 5\n\nA multi root nested set implementation for DBAL users.\n\n## Description\n\nThis library provides you write, read and inspection classes for nested sets with multiple root nodes per table. Solely relying on the Doctrine DBAL.\n\nContrary to other solutions this library has clear boundaries and leaves the software design up to you.\n\n * No tree abstraction - just nested sets\n * No entities - just method calls with plain parameters\n\n## Installation\n\nUse composer to install the library\n\n```sh\n\u003e composer require shopware/dbal-nested-set\n\n```\n\n## Usage\n\nYou always need a configuration that sets up the basic column names of your implementation:\n\n```php\n\nuse Shopware\\DbalNestedSet\\NestedSetConfig;\n\n$config = new NestedSetConfig(\n    'id', // Primary key column name\n    'left', // left column name\n    'right',  // right column name\n    'level' // level column name\n);\n```\n\nThen you can use the `NestedSetFactory` to create the different classes of the library.\n\n```php\nuse Shopware\\DbalNestedSet\\NestedSetFactory;\nuse Doctrine\\DBAL\\Connection;\n\n$writer = NestedSetFactory::createWriter($dbalConnection, $config);\n\n```\n\n### Create a tree\n\nYou 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:\n\n```php\n$tableFactory = NestedSetFactory::createTableFactory($connection, $config);\n\n$schema = new \\Doctrine\\DBAL\\Schema\\Schema();\n$table = $tableFactory-\u003ecreateTable(\n    $schema,\n    'tree', // table name\n    'root_id' // nested set root id\n);\n$table-\u003eaddColumn('id', 'integer', ['unsigned' =\u003e true, 'autoincrement' =\u003e true]);\n$table-\u003eaddColumn('name', 'string', ['length' =\u003e 255]);\n$table-\u003esetPrimaryKey(['id']);\n\n$addSql = $schema-\u003etoSql($connection-\u003egetDatabasePlatform());\n```\n\nOf course this is optional and may be accomplished through any schema configuration tool.\n\n### Modify the tree\n\nThe 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.\n\nAs an example you can use this to create a tree\n\n```php\n\n$writer = NestedSetFactory::createWriter($dbalConnection, $config);\n\n// create a Root node\n$writer-\u003einsertRoot('tree', 'root_id', 100, ['name' =\u003e 'Clothing']);\n\n// create subnodes\n$writer-\u003einsertAsFirstChild('tree', 'root_id', 1, ['name' =\u003e 'Men']);\n$writer-\u003einsertAsNextSibling('tree', 'root_id', 2, ['name' =\u003e 'Women']);\n$writer-\u003einsertAsFirstChild('tree', 'root_id', 2, ['name' =\u003e 'Suits']);\n$writer-\u003einsertAsFirstChild('tree', 'root_id', 3, ['name' =\u003e 'Dresses']);\n$writer-\u003einsertAsNextSibling('tree', 'root_id', 5, ['name' =\u003e 'Skirts']);\n$writer-\u003einsertAsNextSibling('tree', 'root_id', 6, ['name' =\u003e 'Blouses']);\n$writer-\u003einsertAsFirstChild('tree', 'root_id', 4, ['name' =\u003e 'Jackets']);\n$writer-\u003einsertAsFirstChild('tree', 'root_id', 4, ['name' =\u003e 'Slacks']);\n$writer-\u003einsertAsFirstChild('tree', 'root_id', 5, ['name' =\u003e 'Evening Gowns']);\n$writer-\u003einsertAsNextSibling('tree', 'root_id', 10, ['name' =\u003e 'Sun Dresses']);\n```\n\nAnd then use the writer to move nodes around\n\n```php\n$writer-\u003emoveAsNextSibling('tree', 'root_id', 4, 7);\n```\n\n### Inspect nodes\n\nYou may want to retrieve information about different nodes. This can be done through the `NestedSetTableNodeInspector`.\n\n```php\n$inspector = NestedSetFactory::createTableNodeInspector($connection, $config);\n\n$inspector-\u003eisLeaf('tree', 'root_id', 9); // true | false\n$inspector-\u003eisAncestor('tree', 'root_id', 1, 2) // true | false\n```\n\n### Inspect the tree\n\nThe `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.\n\n```php\n$queryFactory = NestedSetFactory::createQueryFactory($connection, $config);\n$data = $queryFactory\n            -\u003ecreateChildrenQueryBuilder('tree', 't', 'root_id', 2)\n            -\u003eselect('*')\n            -\u003eexecute()\n            -\u003efetchAll();\n```\n\n## Local development\n\nIf you want to develop locally you may have to configure the database access through a little shell script:\n\n```bash\n#!/usr/bin/env bash\n\nexport DB_USER='foo'\nexport DB_PASSWORD='bar'\nexport DB_HOST='baz'\nexport DB_NAME='dbal_nested_set'\n\nbin/phpunit\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshopware5%2Fdbal-nested-set","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshopware5%2Fdbal-nested-set","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshopware5%2Fdbal-nested-set/lists"}