1 # vim: ts=8:sw=4:sts=4:et
2 package DBIx::Class::Tree::AdjacencyList::Positional;
5 use base qw( DBIx::Class );
8 __PACKAGE__->load_components(qw(
15 DBIx::Class::Tree::AdjacencyList::Positional - Glue DBIx::Class::Positional and DBIx::Class::Tree::AdjacencyList together. (EXPERIMENTAL)
19 Create a table for your tree data.
21 CREATE TABLE employees (
22 employee_id INTEGER PRIMARY KEY AUTOINCREMENT,
23 parent_id INTEGER NOT NULL,
24 position INTEGER NOT NULL,
28 In your Schema or DB class add Tree::AdjacencyList::Positional
29 to the top of the component list.
31 __PACKAGE__->load_components(qw( Tree::AdjacencyList::Positional ... ));
33 Specify the column that contains the parent ID and position of each row.
36 __PACKAGE__->parent_column('parent_id');
37 __PACAKGE__->position_column('position');
39 This module provides a few extra methods beyond what
40 L<DBIx::Class::Positional> and L<DBIx::Class::Tree::AdjacencyList>
43 my $parent = $employee->parent();
44 $employee->parent( $parent_obj );
45 $employee->parent( $parent_id );
47 my $children_rs = $employee->children();
48 my @children = $employee->children();
50 $parent->append_child( $child );
51 $parent->prepend_child( $child );
53 $this->attach_before( $that );
54 $this->attach_after( $that );
58 This module provides methods for working with adjacency lists and positional
59 rows. All of the methods that L<DBIx::Class::Positional> and
60 L<DBIx::Class::Tree::AdjacencyList> provide are available with this module.
67 my $parent = $employee->parent();
68 $employee->parent( $parent_obj );
69 $employee->parent( $parent_id );
71 my $children_rs = $employee->children();
72 my @children = $employee->children();
74 This method works exactly like it does in the
75 DBIx::Class::Tree::AdjacencyList module except that it will
76 first move the object to the last position of the list, change
77 the parent ID, then move the object to the last position of
78 the new list. This ensures the intergrity of the positions.
83 my( $self, $new_parent ) = @_;
85 if (ref($new_parent)) {
86 $new_parent = $new_parent->id() || croak('Parent object does not have an id');
88 return 0 if ($new_parent == ($self->get_column($self->parent_column())||0));
90 return 0 if (!$self->next::method( $new_parent ));
92 $self->position_column() =>
93 $self->result_source->resultset->search(
94 {$self->_collection_clause()}
101 return $self->next::method();
107 my $children_rs = $employee->children();
108 my @children = $employee->children();
110 This method works just like it does in the
111 DBIx::Class::Tree::AdjacencyList module except it
112 orders the children by there position.
118 my $rs = $self->result_source->resultset->search(
119 { $self->parent_column() => $self->id() },
120 { order_by => $self->position_column() }
122 return $rs->all() if (wantarray());
128 $parent->append_child( $child );
130 Sets the child to have the specified parent and moves the
131 child to the last position.
136 my( $self, $child ) = @_;
137 $child->parent( $self );
142 $parent->prepend_child( $child );
144 Sets the child to have the specified parent and moves the
145 child to the first position.
150 my( $self, $child ) = @_;
151 $child->parent( $self );
152 $child->move_first();
157 $this->attach_before( $that );
159 Attaches the object at the position just before the
160 calling object's position.
165 my( $self, $sibling ) = @_;
166 $sibling->parent( $self->parent() );
167 $sibling->move_to( $self->get_column($self->position_column()) );
172 $this->attach_after( $that );
174 Attaches the object at the position just after the
175 calling object's position.
180 my( $self, $sibling ) = @_;
181 $sibling->parent( $self->parent() );
182 $sibling->move_to( $self->get_column($self->position_column()) + 1 );
185 =head1 PRIVATE METHODS
187 These methods are used internally. You should never have the
190 =head2 collection_column
192 Postional's collection_column method does not, and should not, be
193 defined when using this module. This method just throws out an
194 error if you try to use it.
198 sub collection_column {
199 croak('Use parent_column() instead of collection_column()');
202 =head2 _collection_clause
204 This method is provided as an override of the method in
205 L<DBIx::Class::Positional>. This method is what provides the
206 glue between AdjacencyList and Positional.
210 sub _collection_clause {
213 $self->parent_column() =>
214 $self->get_column($self->parent_column())
223 Aran Clary Deltac <bluefeet@cpan.org>
227 You may distribute this code under the same terms as Perl itself.