1 package DBIx::Class::Tree::AdjacencyList::Ordered;
2 # vim: ts=8:sw=4:sts=4:et
7 use base qw( DBIx::Class );
10 __PACKAGE__->load_components(qw(
17 DBIx::Class::Tree::AdjacencyList::Ordered - Glue DBIx::Class::Ordered and DBIx::Class::Tree::AdjacencyList together.
21 Create a table for your tree data.
24 item_id INTEGER PRIMARY KEY AUTOINCREMENT,
25 parent_id INTEGER NOT NULL DEFAULT 0,
26 position INTEGER NOT NULL,
30 In your Schema or DB class add Tree::AdjacencyList::Ordered
31 to the front of the component list.
33 __PACKAGE__->load_components(qw( Tree::AdjacencyList::Ordered ... ));
35 Specify the column that contains the parent ID and position of each row.
38 __PACKAGE__->position_column('position');
39 __PACKAGE__->parent_column('parent_id');
41 This module provides a few extra methods beyond what
42 L<DBIx::Class::Ordered> and L<DBIx::Class::Tree::AdjacencyList>
45 my $parent = $item->parent();
46 $item->parent( $parent_obj );
47 $item->parent( $parent_id );
49 my $children_rs = $item->children();
50 my @children = $item->children();
52 $parent->append_child( $child );
53 $parent->prepend_child( $child );
55 $this->attach_before( $that );
56 $this->attach_after( $that );
60 This module provides methods for working with adjacency lists and ordered
61 rows. All of the methods that L<DBIx::Class::Ordered> and
62 L<DBIx::Class::Tree::AdjacencyList> provide are available with this module.
68 __PACKAGE__->parent_column('parent_id');
70 Works the same as AdjacencyList's parent_column() method, but it
71 declares the children() has many relationship to be ordered by the
78 my $position_col = $class->position_column() || croak('You must call position_column() before calling parent_column()');
80 $class->grouping_column( @_ );
81 $class->next::method( @_ );
82 $class->relationship_info('children')->{attrs}->{order_by} = $position_col;
85 return $class->grouping_column;
90 my $parent = $item->parent();
91 $item->parent( $parent_obj );
92 $item->parent( $parent_id );
94 This method overrides AdjacencyList's parent() method but
95 modifies it so that the object is moved to the last position,
96 then the parent is changed, and then it is moved to the last
97 position of the new list, thus maintaining the intergrity of
105 my $new_parent = shift;
106 my $parent_col = $self->_parent_column();
107 if (ref($new_parent)) {
108 $new_parent = $new_parent->id() || croak('Parent object does not have an ID');;
110 return 0 if ($new_parent == ($self->get_column($parent_col)||0));
112 $self->set_column( $parent_col => $new_parent );
114 $self->position_column() =>
115 $self->result_source->resultset->search(
116 {$self->_grouping_clause()}
122 return $self->_parent();
127 my $children_rs = $item->children();
128 my @children = $item->children();
130 This method works just like it does in the
131 DBIx::Class::Tree::AdjacencyList module except it
132 orders the children by there position.
136 $parent->append_child( $child );
138 Sets the child to have the specified parent and moves the
139 child to the last position.
144 my( $self, $child ) = @_;
145 $child->parent( $self );
150 $parent->prepend_child( $child );
152 Sets the child to have the specified parent and moves the
153 child to the first position.
158 my( $self, $child ) = @_;
159 $child->parent( $self );
160 $child->move_first();
165 $this->attach_before( $that );
167 Attaches the object at the position just before the
168 calling object's position.
173 my( $self, $sibling ) = @_;
174 $sibling->parent( $self->parent() );
175 $sibling->move_to( $self->get_column($self->position_column()) );
180 $this->attach_after( $that );
182 Attaches the object at the position just after the
183 calling object's position.
188 my( $self, $sibling ) = @_;
189 $sibling->parent( $self->parent() );
190 $sibling->move_to( $self->get_column($self->position_column()) + 1 );
196 =head1 INHERITED METHODS
198 =head2 DBIx::Class::Ordered
204 L<siblings|DBIx::Class::Ordered/siblings>
208 L<first_sibling|DBIx::Class::Ordered/first_sibling>
212 L<last_sibling|DBIx::Class::Ordered/last_sibling>
216 L<previous_sibling|DBIx::Class::Ordered/previous_sibling>
220 L<next_sibling|DBIx::Class::Ordered/next_sibling>
224 L<move_previous|DBIx::Class::Ordered/move_previous>
228 L<move_next|DBIx::Class::Ordered/move_next>
232 L<move_first|DBIx::Class::Ordered/move_first>
236 L<move_last|DBIx::Class::Ordered/move_last>
240 L<move_to|DBIx::Class::Ordered/move_to>
244 L<insert|DBIx::Class::Ordered/insert>
248 L<delete|DBIx::Class::Ordered/delete>
252 =head2 DBIx::Class::Tree::AdjacencyList
258 L<parent_column|DBIx::Class::Tree::AdjacencyList/parent_column>
262 L<parent|DBIx::Class::Tree::AdjacencyList/parent>
266 L<attach_child|DBIx::Class::Tree::AdjacencyList/attach_child>
270 L<siblings|DBIx::Class::Tree::AdjacencyList/siblings>
274 L<attach_sibling|DBIx::Class::Tree::AdjacencyList/attach_sibling>
284 L<mk_classdata|DBIx::Class/mk_classdata>
288 L<component_base_class|DBIx::Class/component_base_class>
292 =head2 DBIx::Class::Componentised
298 L<inject_base|DBIx::Class::Componentised/inject_base>
302 L<load_components|DBIx::Class::Componentised/load_components>
306 L<load_own_components|DBIx::Class::Componentised/load_own_components>
310 =head2 Class::Data::Accessor
316 L<mk_classaccessor|Class::Data::Accessor/mk_classaccessor>
322 Aran Clary Deltac <bluefeet@cpan.org>
326 You may distribute this code under the same terms as Perl itself.