The document outlines Eli Aschkenasy's presentation on nested data sets. The agenda includes an introduction, explanation of nested data sets, types of hierarchies, using nested sets in Joomla, basic functions, advanced functions, and "ten commandments". Examples are provided of SQL queries to find the root node, leaf nodes, and basic functions in Joomla for working with nested data sets.
2. INTRODUCTION
Eli Aschkenasy
themodularway.com
Oracle certified (doesn’t really mean anything)
GE sourcing database project
Agenda
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 2
3. AGENDA
Introduction (5min)
Simple Explanation of Nested Data Sets (10min)
Types of Hierarchies (5min)
The Joomla Way (5min)
Basic Functions (15min)
Advanced Functions (10min)
The Ten Commandments (for ever…)
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 3
4. WELCOME TO OUR DEMO TEAM (COURTESY OF MS POWERPOINT TEMPLATES)
Terry Earls
Charlotte
Weiss
Rob
Walters
Manuel
Oliveira
Dan
Wilson
Chen
Yang
Phyllis
Harris
Mark
Hanson
Maureen
Magnotta
Patricia
Doyle
Mike
Nash
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 4
13. AGENDA
Introduction (5min)
Simple Explanation of Nested Data Sets (10min)
Types of Hierarchies (5min)
The Joomla Way (5min)
Basic Functions (15min)
Advanced Functions (10min)
The Ten Commandments (for ever…)
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 6
14. TYPES OF HIERARCHIES
Static nodes and static edges
A chart of accounts in an accounting system will
probably not change much over time.
This is probably best done with a hierarchical
encoding scheme rather than a table.
Static nodes and dynamic edges
For example, an Internet Newsgroup message
board. Obviously you cannot add a node to a
tree without adding an edge, but the content of
the messages (nodes) never change once they
are posted; however, new replies can be posted
as subordinates to any existing message (edge).
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 7
15. TYPES OF HIERARCHIES
Dynamic nodes and static edges
This is the classic organizational chart in which
the organization stays the same, but the people
holding the offices rotate frequently.
This is assuming that your company does not
reorganize more often than its personnel turns
over.
Dynamic nodes and dynamic edges
The fastest path from the fire station to a
particular home address will not necessarily be
the same route at 5:00 AM as it will be at 5:00
PM.
Once the fire is put out, the node that
represented the burning house can disappear
from the tree and the next fire location becomes
a to which we must find a path.
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 8
16. TYPES OF HIERARCHIES
Dynamic nodes and static edges
This is the classic organizational chart in which
the organization stays the same, but the people
holding the offices rotate frequently.
This is assuming that your company does not
reorganize more often than its personnel turns
over.
Dynamic nodes and dynamic edges
The fastest path from the fire station to a
particular home address will not necessarily be
the same route at 05:00 Hrs. as it will be at 17:00
Hrs.
Once the fire is put out, the node that
represented the burning house can disappear
from the tree and the next fire location becomes
a to which we must find a path.
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 9
17. AGENDA
Introduction (5min)
Simple Explanation of Nested Data Sets (10min)
Types of Hierarchies (5min)
The Joomla Way (5min)
Basic Functions (15min)
Advanced Functions (10min)
The Ten Commandments (for ever…)
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 10
18. THE JOOMLA WAY - JTableNested
http://docs.joomla.org/Using_nested_sets
Table creation (required fields)
CREATE TABLE IF NOT EXISTS `#__nestedsets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
`lft` int(11) NOT NULL DEFAULT '0',
`rgt` int(11) NOT NULL DEFAULT '0',
`level` int(10) UNSIGNED NOT NULL DEFAULT '0',
`title` varchar(255) NOT NULL,
`alias` varchar(255) NOT NULL DEFAULT '',
`access` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
`path` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `idx_left_right` (`lft`,`rgt`)
) DEFAULT CHARSET=utf8;
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 11
38. ADVANCED FUNCTIONS
Children to Parent
Mom dies and Grandma adopts the kids.
In effect the position itself is removed.
This is a vertical promotion of an entire subtree.
Child node to the deleted node’s
position
Give the business to the oldest son.
The problem is that when the son is promoted,
this leaves a vacancy in his former position.
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 27
Deleting Nodes (J is simple (?))
39. ADVANCED FUNCTIONS
Sibling moves over to the vacant
position
Dad dies and his oldest brother takes over the
business.
This assumes that there is such a brother to take
the vacant position.
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 28
Deleting Nodes (J is simple (?))
40. ADVANCED FUNCTIONS
Terry Earls
Charlotte
Weiss
Rob
Walters
Manuel
Oliveira
Dan
Wilson
Chen
Yang
Phyllis
Harris
Mark
Hanson
Maureen
Magnotta
Patricia
Doyle
Mike
Nash
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - THEMODULARWAY 29
1
2
3 4 5 6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
8
1
9
1
7
1
6
2
0
21
22
Deleting Nodes (J is simple (?))
// Delete the node and all of its children.
$query = $this->_db->getQuery(true);
$query->delete();
$query->from($this->_tbl);
$query->where('lft BETWEEN ' . (int) $node->lft . ' AND ' . (int) $node->rgt);
$this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
// Compress the left values.
$query = $this->_db->getQuery(true);
$query->update($this->_tbl);
$query->set('lft = lft - ' . (int) $node->width);
$query->where('lft > ' . (int) $node->rgt);
$this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
// Compress the right values.
$query = $this->_db->getQuery(true);
$query->update($this->_tbl);
$query->set('rgt = rgt - ' . (int) $node->width);
$query->where('rgt > ' . (int) $node->rgt);
$this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
41. ADVANCED FUNCTIONS
Find width of subtree: (rgt-lft)+1
Find offset of subtree: (old lft-new left)
Store original lft
Add width to lft and rgt where lft>=new left (creating gap)
Change lft and rgt of subtree to lft-offset and rgt-offset where lft between cur_lft and cur_left+width
(filling gap)
Subtract width from lft and rgt where lft>=new lft+width (connecting right)
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 30
Moving Subtrees (J is simple)
47. AGENDA
Introduction (5min)
Simple Explanation of Nested Data Sets (10min)
Types of Hierarchies (5min)
The Joomla Way (5min)
Basic Functions (15min)
Advanced Functions (10min)
The Ten Commandments
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 35
48. TEN COMMANDMENTS
I am not the Lord thy God, test on your own
Thou shalt think if Nested Sets are what you
need
Thou shalt utilize native Joomla! if adequate
Thou must add index to rgt
Honor thy table locks
Thou shalt think about a-synch data
Thou shalt separate entities from hierarchy
Hierarchy Leafs are still categories (Thou shalt)
Thou shalt allow entities belonging to many
nodes
JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 36
49. JOOMLADAY NETHERLANDS 2013 - ELI ASCHKENASY - @ELIASCHKENASY 37
HET EINDE
ja, ik spreek vloeiend nederlands...