Leser: 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
sub insert_node {
my ($class, $rs, $id, $data) = @_;
my $var = $class . "::_nested_set";
no strict 'refs';
my ($left_id, $right_id, $parent_id) = (1, 2, 0);
$data->{title} = 'root' unless defined $data->{title};
if ($id) {
my $cat = $rs->find($id) or return;
my $upper = $rs->search({
left_id => { '<=' => $cat->left_id },
right_id => { '>=' => $cat->right_id },
});
$upper->update({
right_id => \'right_id + 2',
});
my $right = $rs->search({
left_id => { '>' => $cat->left_id },
right_id => { '>' => $cat->right_id },
});
$right->update({
left_id => \'left_id + 2',
right_id => \'right_id + 2',
});
($left_id, $right_id, $parent_id) = ($cat->right_id, $cat->right_id + 1, $cat->id);
}
my $node = $rs->create({
%$data,
left_id => $left_id,
right_id => $right_id,
parent_id => $parent_id,
});
return $node;
}
($left_id, $right_id, $parent_id) = ($cat->right_id, $cat->right_id + 1, $cat->id);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
ausgangslage:
id lft rgt
1 1 8
2 2 3
3 4 7
4 5 6
unter node 4 soll ein neuer node hinzugefügt werden.
($left_id, $right_id, $parent_id) = ($cat->right_id, $cat->right_id + 1, $cat->id);
= (6, 7, 4)
$upper wird ermittelt:
id lft rgt
1 1 8 *
2 2 3
3 4 7 *
4 5 6 *
und der rgt (right_id) um 2 hochgezählt:
id lft rgt
1 1 10
2 2 3
3 4 9
4 5 8
$right wird ermittelt, ist in diesem fall leer.
neuer node bekommt lft=6, rgt=7:
id lft rgt
1 1 10
2 2 3
3 4 9
4 5 8
*5 6 7
1
2
3
4
5
6
7
8
9
10
11
12
13
=head2 insert_node( $rs, $id?, \%data? )
Insert a new first node in $rs.
In case $id is given, the new node will be inserted as last node in the node
identified by $id (now refered as node($id).
Additional %data of the new node can be given as hashref.
By default, if no $id is given, this inserts a root node. The ident of the root
node wil be 'root'.
=cut