{"id":16795587,"url":"https://github.com/nineinchnick/closure-table-manager","last_synced_at":"2025-04-11T00:06:39.354Z","repository":{"id":14178885,"uuid":"16885071","full_name":"nineinchnick/closure-table-manager","owner":"nineinchnick","description":"PHP library that helps maintain adjacency list SQL structures","archived":false,"fork":false,"pushed_at":"2014-07-08T12:27:55.000Z","size":196,"stargazers_count":10,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-11T00:06:34.134Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/nineinchnick.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":"2014-02-16T13:15:07.000Z","updated_at":"2022-03-12T11:04:55.000Z","dependencies_parsed_at":"2022-07-10T00:16:39.165Z","dependency_job_id":null,"html_url":"https://github.com/nineinchnick/closure-table-manager","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nineinchnick%2Fclosure-table-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nineinchnick%2Fclosure-table-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nineinchnick%2Fclosure-table-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nineinchnick%2Fclosure-table-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nineinchnick","download_url":"https://codeload.github.com/nineinchnick/closure-table-manager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317707,"owners_count":21083528,"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":[],"created_at":"2024-10-13T09:16:48.718Z","updated_at":"2025-04-11T00:06:39.334Z","avatar_url":"https://github.com/nineinchnick.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"closure-table-manager\n=====================\n\nPHP library that helps maintain adjacency list SQL structures.\n\nTL;DR: It allows fetching all ancestors/descendants (indirect parents/children) in a single query, without using recursive queries.\n\nInspired by:\n* [SQL::Tree Perl module](https://github.com/mlawren/sqltree)\n* http://www.depesz.com/index.php/2008/04/11/my-take-on-trees-in-sql/\n\nCurrently supported databases:\n* PostgreSQL\n* SQLite 3\n* MySQL and MariaDB\n\nPull requests with other databases support are very welcome.\n\n## Installation\n\nUsing composer:\n\n~~~bash\ncurl -sS https://getcomposer.org/installer | php\n./composer.phar require nineinchnick/closure-table-manager:dev-master\n~~~\n\n## Usage\n\nCall `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.\n\nWhen installed, triggers will block the following operations:\n* Changing the primary key value\n* Creating loops\n\nA command line script is provided:\n~~~\nUsage: ./vendor/bin/closureTable.php [options] [operands]\nOptions:\n  -d, --dsn \u003carg\u003e         DSN connection string or just the driver name (pgsql, sqlite, mysql).\n  -t, --table \u003carg\u003e       Table name.\n  -p, --parent \u003carg\u003e      Parent foreign key column name.\n  -i, --pk \u003carg\u003e          Primary key column name.\n  --pk_type \u003carg\u003e          Primary key and parent column type.\n  --path \u003carg\u003e            Path column name; if set, additional triggers will be generated.\n  --path_from \u003carg\u003e       Column which value will be used to build a path. Its values cant't contain path_separator.\n  --path_separator \u003carg\u003e  Path separator character.\n  --table_suffix \u003carg\u003e    Suffix of the closure table.\n~~~\n\n## Example\n\nHaving the following tables:\n\n~~~sql\nCREATE TABLE products (\n  id INTEGER,\n  category_id INTEGER NOT NULL REFERENCES categories (id),\n  -- ...\n  PRIMARY KEY(id)\n);\n\nCREATE TABLE categories (\n  id INTEGER,\n  parent_id INTEGER NOT NULL REFERENCES categories (id),\n  -- ...\n  PRIMARY KEY(id)\n);\n~~~\n\nIt is quite common to ask database for all products in given category and it's subcategories.\n\n~~~sql\n    SELECT p.*\n      FROM products p\nINNER JOIN categories_tree c on p.category_id = c.id\n     WHERE c.parent_id = \u003cSOME_ID\u003e;\n~~~\n\nWhen user is _in_ some category, we would like to show him _path_ to this category. So he could easily move to some parent category.\n\n~~~sql\n    SELECT c.*\n      FROM categories c\nINNER JOIN categories_tree t on c.id = t.parent_id\n     WHERE c.id = 4\n  ORDER BY t.depth DESC;\n~~~\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnineinchnick%2Fclosure-table-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnineinchnick%2Fclosure-table-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnineinchnick%2Fclosure-table-manager/lists"}