in reply to duplicates in DB tree
if the table categories is not created from scratch this will not work. So I have to find also answer to this question, anyone can help me :)use strict; use warnings; use DBIx::Tree::NestedSet; use DBI; use Data::Dumper; $Data::Dumper::Indent = 1; my $dbh = DBI->connect('DBI:mysql:test','root','') or die ($DBI::errstr); my $tree = DBIx::Tree::NestedSet->new( dbh => $dbh, no_locking => 1, table_name => 'db_tree' ); $dbh->do("DELETE from db_tree"); #we can't have more "top" categories, so we create "main" $tree->add_child_to_right( name=>'main' ); my %tree = (); while (<DATA>) { chomp; my $hr; $hr->{last_node_id} = 0; my $t = \%tree; for my $item ( split '/' ) { #if path doesn't exists in hash unless ( exists $t->{$item} ) { #add to db $hr->{last_id} = $tree->add_child_to_right( name => $item, id=>$hr->{last_node_id} ); #make data hash structure $t->{$item} = { 'node_id' => $hr->{last_id} } ; } $hr->{last_node_id} = $t->{$item}{node_id}; print $hr->{last_node_id}, "\n"; $t = \%{$t->{$item}}; } } print "\n", Dumper(\%tree), "\n"; print "\nThe Complete Tree:\n"; print $tree->create_report(); __DATA__ foo/bar bar/foo foo/bar/foo foo/bar/foo/bar/bar/foo foo/bar/bar/bar
|
|---|