PHP Classes

Closure Table: Manage transitive closure tables stored in MySQL

Recommend this page to a friend!
  Info   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 950 All time: 3,790 This week: 455Up
Version License PHP version Categories
closuretable 0.1The PHP License5.0PHP 5, Databases, Data types
Description 

Author

This package can be used to manage transitive closure tables (TCT) stored in a MySQL database.

It can create MySQL database tables that will store information about hierarchies of nodes stored in other database tables.

Different classes can organize nodes hierarchically by weight and depth. They can insert, delete, retrieve and sort nodes.

Innovation Award
PHP Programming Innovation award nominee
July 2009
Number 8


Prize: One book of choice by Manning
Databases are often used to store records of information that are related in ways that form hierarchic trees and other types of graphs.

There are solutions to manipulate sets records that form these types of graph, like the Joe Celko's nested sets.

However, such solutions require that you have additional fields in the records to store information about the graph structures.

Using closure tables is a different approach that consists in using separate tables to store graph structural information.

This class implements an API that manages closure tables in PHP. It can be used to manage graphs of records of information created by existing PHP applications without changing the code that manages the application table records.

Manuel Lemos
Picture of Tom Schaefer
  Performance   Level  
Innovation award
Innovation award
Nominee: 9x

Winner: 1x

 

Details

# MySQL Transitive Closure Tables (TCT) or Adjacency Relations Every one knows nested sets, or materialized path and adjacency lists to structure data hierarchically in relational databases. This is my approach to work with a fourth kind of hierarchy models. If you know something about graph theory, then you will immediately understand what the aim of my approach is. You can easily feed graph applications with data from a mysql database. TCT organize nodes hierarchically by weight and depth. TCT are physically separated from content data tables. TCT administer structures (node, edge), not content. TCT work like nested sets. It is possible to compute nested set left and right values programmatically. Although DML operations are faster than nested set operations, there is a lack. TCT waste db space because each node is completely resolved to represent all related edges. Conclusion: Retrieving TCT data is as fast as nested sets retrievals. TCT DML real-time operations are much faster. There is no need to update myriads of left and right values. ## Requirements - php 5+ - mysql ## Sample Installation - create db mydb and insert sample sql from file - change connection data in samples/config.inc.php to access db - call samples/ClosureTable/index.php (here resides the app) - Db class comes with an interface, so you can easily implement your own db wrapper !!! use create table operations when you know what you are doing !!! ## Db (Database) classes ### Basic features The TCT comes within some basic table structures: - first level: ancestor - descendant - second level: additional depth - third level: additional weight Depending on the goal of a relation one of the three table layouts may be chosen. For simple org charts a first level design is recommended. Third level designs are used for TCT with weighted nodes (sorting algo.); second level layouts are only sets of relations which allow easy access to node trees by depth. ClosureTable offers interface methods which capsule all available commands. ### Retrieving data Class ClosureTableRetrieve contains common queries to retrieve hierarchical lists. To get a node list record set which represent the full path, use ClosureTableRetrieve::getPath(). Its reverse function is ClosureTableRetrieve::getUpPath(). The class provides some slightly deviating methods with different aims. You can query node lists in different ways. ClosureTableRetrieve::getTree() returns the full node tree. The class also provides four retrieve sibling methods. ### Inserting data ClosureTableInsert has only a single method: insert(). Insert inserts and relates nodes as last child of its parent. ### Deleting data ClosureTableDelete comes a long with two functions: - delete() removes a leaf - deleteSubtree() removes a node and its decendants ## Sorting data ClosureTableSort consists of node movement operations: - moveLeft() brings a node one level up - moveRight() brings a node one level down to its previos sibling - moveUp() sets a node before its previous sibling - moveDown() sets a node before its next sibling ## Tool The tool ClosureTable contains sample tree build mechanisms. On default a closure relation is represented as a nested list of unordered items. The tree generator method attaches basic actions to each node item of the tree. Actions: - add a new node = ?perm=add&a={nodeid}&node={parentnodeid} - zoom into subtree = ?node={nodeid} - move a node up: - up = ?perm=up&a={nodeid}&p={parentnodeid} - down = ?perm=down&a={nodeid}&p={parentnodeid} - left = ?perm=left&a={nodeid}&p={parentnodeid} - right = ?perm=right&a={nodeid}&p={parentnodeid} - delete: - node = ?perm=del&a={nodeid}&p={parentnodeid} - subtree = ?perm=rem&a={nodeid}&p={parentnodeid}

Screenshots (1)  
  • sample.png
  Files folder image Files (35)  
File Role Description
Files folder imageimg (11 files)
Files folder imageDb (10 files)
Files folder imageTool (1 file, 2 directories)
Files folder imagesamples (1 file, 1 directory)
Accessible without login Plain text file index.php Aux. load classes
Accessible without login Plain text file Readme.txt Doc. Readme
Accessible without login Plain text file sample.sql Data sample db layout

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:950
This week:0
All time:3,790
This week:455Up
User Comments (1)
class doesn't work.
13 years ago (Chris Novoa)
50%StarStarStar