1 # vim: ts=8:sw=4:sts=4:et
2 package DBIx::Class::Tree::AdjacencyList::Ordered;
5 use base qw( DBIx::Class );
8 __PACKAGE__->load_components(qw(
15 DBIx::Class::Tree::AdjacencyList::Ordered - Glue DBIx::Class::Ordered and DBIx::Class::Tree::AdjacencyList together. (EXPERIMENTAL)
19 Create a table for your tree data.
22 item_id INTEGER PRIMARY KEY AUTOINCREMENT,
23 parent_id INTEGER NOT NULL DEFAULT 0,
24 position INTEGER NOT NULL,
28 In your Schema or DB class add Tree::AdjacencyList::Ordered
29 to the front of the component list.
31 __PACKAGE__->load_components(qw( Tree::AdjacencyList::Ordered ... ));
33 Specify the column that contains the parent ID and position of each row.
36 __PACKAGE__->position_column('position');
37 __PACKAGE__->parent_column('parent_id');
39 This module provides a few extra methods beyond what
40 L<DBIx::Class::Ordered> and L<DBIx::Class::Tree::AdjacencyList>
43 my $parent = $item->parent();
44 $item->parent( $parent_obj );
45 $item->parent( $parent_id );
47 my $children_rs = $item->children();
48 my @children = $item->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 ordered
59 rows. All of the methods that L<DBIx::Class::Ordered> and
60 L<DBIx::Class::Tree::AdjacencyList> provide are available with this module.
66 __PACKAGE__->parent_column('parent_id');
68 Works the same as AdjacencyList's parent_column() method, but it
69 declares the children() has many relationship to be ordered by the
76 my $position_col = $class->position_column() || croak('You must call position_column() before calling parent_column()');
78 $class->grouping_column( @_ );
79 $class->next::method( @_ );
80 $class->relationship_info('children')->{attrs}->{order_by} = $position_col;
83 return $class->grouping_column;
88 my $parent = $item->parent();
89 $item->parent( $parent_obj );
90 $item->parent( $parent_id );
92 This method overrides AdjacencyList's parent() method but
93 modifies it so that the object is moved to the last position,
94 then the parent is changed, and then it is moved to the last
95 position of the new list, thus maintaining the intergrity of
103 my $new_parent = shift;
104 my $parent_col = $self->_parent_column();
105 if (ref($new_parent)) {
106 $new_parent = $new_parent->id() || croak('Parent object does not have an ID');;
108 return 0 if ($new_parent == ($self->get_column($parent_col)||0));
110 $self->set_column( $parent_col => $new_parent );
112 $self->position_column() =>
113 $self->result_source->resultset->search(
114 {$self->_grouping_clause()}
120 return $self->_parent();
125 my $children_rs = $item->children();
126 my @children = $item->children();
128 This method works just like it does in the
129 DBIx::Class::Tree::AdjacencyList module except it
130 orders the children by there position.
134 $parent->append_child( $child );
136 Sets the child to have the specified parent and moves the
137 child to the last position.
142 my( $self, $child ) = @_;
143 $child->parent( $self );
148 $parent->prepend_child( $child );
150 Sets the child to have the specified parent and moves the
151 child to the first position.
156 my( $self, $child ) = @_;
157 $child->parent( $self );
158 $child->move_first();
163 $this->attach_before( $that );
165 Attaches the object at the position just before the
166 calling object's position.
171 my( $self, $sibling ) = @_;
172 $sibling->parent( $self->parent() );
173 $sibling->move_to( $self->get_column($self->position_column()) );
178 $this->attach_after( $that );
180 Attaches the object at the position just after the
181 calling object's position.
186 my( $self, $sibling ) = @_;
187 $sibling->parent( $self->parent() );
188 $sibling->move_to( $self->get_column($self->position_column()) + 1 );
194 =head1 INHERITED METHODS
196 =head2 DBIx::Class::Ordered
202 L<siblings|DBIx::Class::Ordered/siblings>
206 L<first_sibling|DBIx::Class::Ordered/first_sibling>
210 L<last_sibling|DBIx::Class::Ordered/last_sibling>
214 L<previous_sibling|DBIx::Class::Ordered/previous_sibling>
218 L<next_sibling|DBIx::Class::Ordered/next_sibling>
222 L<move_previous|DBIx::Class::Ordered/move_previous>
226 L<move_next|DBIx::Class::Ordered/move_next>
230 L<move_first|DBIx::Class::Ordered/move_first>
234 L<move_last|DBIx::Class::Ordered/move_last>
238 L<move_to|DBIx::Class::Ordered/move_to>
242 L<insert|DBIx::Class::Ordered/insert>
246 L<delete|DBIx::Class::Ordered/delete>
250 =head2 DBIx::Class::Tree::AdjacencyList
256 L<parent_column|DBIx::Class::Tree::AdjacencyList/parent_column>
260 L<parent|DBIx::Class::Tree::AdjacencyList/parent>
264 L<attach_child|DBIx::Class::Tree::AdjacencyList/attach_child>
268 L<siblings|DBIx::Class::Tree::AdjacencyList/siblings>
272 L<attach_sibling|DBIx::Class::Tree::AdjacencyList/attach_sibling>
282 L<mk_classdata|DBIx::Class/mk_classdata>
286 L<component_base_class|DBIx::Class/component_base_class>
290 =head2 DBIx::Class::Componentised
296 L<inject_base|DBIx::Class::Componentised/inject_base>
300 L<load_components|DBIx::Class::Componentised/load_components>
304 L<load_own_components|DBIx::Class::Componentised/load_own_components>
308 =head2 Class::Data::Accessor
314 L<mk_classaccessor|Class::Data::Accessor/mk_classaccessor>
320 Aran Clary Deltac <bluefeet@cpan.org>
324 You may distribute this code under the same terms as Perl itself.