Storing Hierarchical Data
Posted: 06 October 2006 07:12 AM   [ Ignore ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1184
Joined  01-05-2006

Hello everyone,

I’ve been reading about hierarchical data lately as I develop a hierarchical relationship field, and was wondering if anyone smarter than me could shed some light on a modified pre-order traversal question I have (read more about it here: http://www.sitepoint.com/article/hierarchical-data-database).

The question being:  If you have more than one root node, how do would you go about traversing your trees.  Even more simplistic, how would you retrieve your root nodes at all to begin the traversal?

Thanks guys, any help, including links of more reading would be very helpful.
Mark

 Signature 

================================================
    Mark Huot
    http://markhuot.com
================================================

Profile
 
 
Posted: 06 October 2006 08:22 AM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15845
Joined  06-03-2002

I normal situations, you won’t need all root nodes and all children (or grandchildren).  I guess that depends on the type of application you’re building.  If you look at EE’s database usage, “parent” and “children” are stored in separate tables, with a shared id.  Now you certainly *could* grab all of the records, stick them into a multidimensional array, and step through the branches.  But in most cases, you need something more specific, or at least know one key bit of information that makes it unnecessary to walk through the entire mess of data (the parent’s id, for example).  In some cases, it might be necessary to include in one table a bit of metadata about the parent/child/grandchild.  It might be better if you could describe your desired data model and some people experienced with MySQL could help you structure it in a way that will make it more intuitive to access.

 Signature 
Profile
MSG
 
 
Posted: 06 October 2006 10:20 AM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1184
Joined  01-05-2006

Well I was looking at this because I am creating a hierarchical relationship field that will display as follows:

Root
  
|---- Child 1
    
|---- Child 1.2
  
|---- Child 2
    
|---- Child 2.1
    
|---- Child 2.2

So what I’m envisioning is that the field_id_x will store the relationship id, just like a normal relationship field would, then, the difference would be, the entry id would get put into a separate table containing the hierarchy of the field.  So that when the field is called in the template it uses the normal relationship code, but when displayed in the CP the user sees a nested list.

So….The reason I need to pull ALL of the data is because when a user is entering an entry they should be able to choose to parent the entry with ANY other entry.  I guess I’m a little confused because there won’t always be one root node, there may be 10 - 1000.

Finally, a little background:  this extension will be used to create a nested list of corporate branches around the world.  So you could log in and enter a new “Branch” (that’s the weblog).  Then you’d fill in all the info along with the branch; address, phone, email etc.  Then the last field is “Branch Parent” which lets you choose, from that nested list, another entry to parent this one to.  So, in this example one might log in and create a branch named “USA” then create a branch named “California”, and parent that branch to “USA”.  At last they may create a branch named “San Francisco”, and parent that branch to “California”.  Then what they would see, if they were to enter another branch (say, Colorado), in that field would be a nested list as follows:

USA
  
|---- California
    
|---- San Francisco

Which is obviously much easier to scan and find the parent you are looking for.

 Signature 

================================================
    Mark Huot
    http://markhuot.com
================================================

Profile
 
 
Posted: 06 October 2006 10:35 AM   [ Ignore ]   [ # 3 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15845
Joined  06-03-2002

Gotcha.  Might get some ideas by looking at the category tables, and how the related functions create the nested lists from umpteen levels of nesting.

 Signature 
Profile
MSG
 
 
Posted: 06 October 2006 10:41 AM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1184
Joined  01-05-2006

I saw that, and I like the idea of pulling everything.  I took it one step further, that when the “categories” are pulled I store them in an array with the ‘parent_id’ as the index.  Then when I recurse through the results I can get any children by calling $categories[$id] because they are stored with the parent as the index.

The reason I was looking at the modified pre-order traversal method, was because I’m going have well over 5000 entries, and was wondering if I could get any better speed that way.

 Signature 

================================================
    Mark Huot
    http://markhuot.com
================================================

Profile
 
 
   
 
 
‹‹ Markdown and Smarty Pants      File ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65029 Total Logged-in Users: 26
Total Topics: 82119 Total Anonymous Users: 18
Total Replies: 441323 Total Guests: 183
Total Posts: 523442    
Members ( View Memberlist )