Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Tree Traversal Script/Nested Sets

by Hero Zzyzzx (Curate)
on Jul 14, 2004 at 14:18 UTC ( #374301=note: print w/ replies, xml ) Need Help??


in reply to Tree Traversal Script/Nested Sets

I'm the author of DBIx::Tree:NestedSet. Out of the box it supports SQLite and MySQL, I'm looking for more database champions to beef up support.

Below is one way you could set up your tree. I did it manually for the sake of verbosity.

Note: I'm about to release a new version of the module (it's currently at 0.12), the most glaring bug it fixes is the broken return value of the various add_* methods.

This module is pretty young, caveat coder. I'm very open to patches and bug fixes, (this module is important to a few apps I've got out there) I won't be abandoning it anytime soon.

#!/usr/bin/perl use DBI; use DBIx::Tree::NestedSet; use Data::Dumper; use strict; my $dbh=DBI->connect('dbi:SQLite:dbname=nested_set') or die($DBI::errs +tr); my $tree=DBIx::Tree::NestedSet->new(dbh=>$dbh,db_type=>'SQLite'); $tree->create_default_table(); $tree->add_child_to_right(term=>'Body Regions'); $tree->add_child_to_right( term=>'Extremities', id=>$tree->get_root() ); my $extremities_id=$tree->get_id_by_key( key_name=>'term', key_value=>'Extremities' ); foreach('Arm','Elbow','Hand'){ $tree->add_child_to_right( term=>$_, id=>$extremities_id ); } my $hand_id=$tree->get_id_by_key( key_name=>'term', key_value=>'Hand' ); $tree->add_child_to_right( id=>$hand_id, term=>'Fingers' ); my $fingers_id=$tree->get_id_by_key( key_name=>'term', key_value=>'Fingers' ); $tree->add_child_to_right( id=>$fingers_id, term=>'Thumbs' ); my $structure=$tree->get_self_and_children_flat(); foreach(@$structure){ print " " x $_->{level}. $_->{term}."\n"; } $dbh->do('drop table nested_set');

-Any sufficiently advanced technology is
indistinguishable from doubletalk.

My Biz


Comment on Re: Tree Traversal Script/Nested Sets
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://374301]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-09-01 12:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (6 votes), past polls