name TEXT NOT NULL,
position INTEGER NOT NULL
);
- # Optional: group_id INTEGER NOT NULL
+
+Optionally, add one or more columns to specify groupings, allowing you
+to maintain independent ordered lists within one table:
+
+ CREATE TABLE items (
+ item_id INTEGER PRIMARY KEY AUTOINCREMENT,
+ name TEXT NOT NULL,
+ position INTEGER NOT NULL,
+ group_id INTEGER NOT NULL
+ );
+
+Or even
+
+ CREATE TABLE items (
+ item_id INTEGER PRIMARY KEY AUTOINCREMENT,
+ name TEXT NOT NULL,
+ position INTEGER NOT NULL,
+ group_id INTEGER NOT NULL,
+ other_group_id INTEGER NOT NULL
+ );
In your Schema or DB class add Ordered to the top
of the component list.
package My::Item;
__PACKAGE__->position_column('position');
- __PACKAGE__->grouping_column('group_id'); # optional
+
+If you are using one grouping column, specify it as follows:
+
+ __PACKAGE__->grouping_column('group_id');
+
+Or if you have multiple grouping columns:
+
+ __PACKAGE__->grouping_column(['group_id', 'other_group_id']);
Thats it, now you can change the position of your objects.
$item->move_first();
$item->move_last();
$item->move_to( $position );
+ $item->move_to_group( 'groupname' );
+ $item->move_to_group( 'groupname', $position );
+ $item->move_to_group( {group_id=>'groupname', 'other_group_id=>'othergroupname'} );
+ $item->move_to_group( {group_id=>'groupname', 'other_group_id=>'othergroupname'}, $position );
=head1 DESCRIPTION
1 is returned on success, and 0 is returned if the object is
already at the specified position of the specified group.
-$group should be supplied as a hashref of column => value pairs,
-e.g. if the grouping columns were 'user' and 'list',
-{ user => 'fred', list => 'work' }.
+$group may be specified as a single scalar if only one
+grouping column is in use, or as a hashref of column => value pairs
+if multiple grouping columns are in use.
=cut
my $pos_col = $self->position_column;
- # is there a chance in hell of this working?
# if any of our grouping columns have been changed
-$DB::single=1;
if (grep {$_} map {exists $changes{$_}} $self->_grouping_columns ) {
# create new_group by taking the current group and inserting changes
=head2 _get_grouping_columns
Returns a list of the column names used for grouping, regardless of whether
-they were specified as an arrayref or a single string, and even returns ()
-if we're not grouping.
+they were specified as an arrayref or a single string, and returns ()
+if there is no grouping.
=cut
sub _grouping_columns {
}
-
-
1;
__END__
use lib qw(t/lib);
use DBICTest;
+use POSIX qw(ceil);
+
my $schema = DBICTest->init_schema();
-plan tests => 417;
+plan tests => 879;
my $employees = $schema->resultset('Employee');
$employees->delete();
"overloaded update 7"
);
+# multicol tests begin here
+DBICTest::Employee->grouping_column(['group_id', 'group_id_2']);
+$employees->delete();
+foreach my $group_id (1..4) {
+ foreach my $group_id_2 (1..4) {
+ foreach (1..4) {
+ $employees->create({ name=>'temp', group_id=>$group_id, group_id_2=>$group_id_2 });
+ }
+ }
+}
+$employees = $employees->search(undef,{order_by=>'group_id,group_id_2,position'});
+
+foreach my $group_id (1..3) {
+ foreach my $group_id_2 (1..3) {
+ my $group_employees = $employees->search({group_id=>$group_id, group_id_2=>$group_id_2});
+ $group_employees->all();
+ ok( check_rs($group_employees), "group intial positions" );
+ hammer_rs( $group_employees );
+ }
+}
+
+# move_to_group, specifying group by hash
+my $group_4 = $employees->search({group_id=>4});
+$to_group = 1;
+my $to_group_2_base = 7;
+my $to_group_2 = 1;
+$to_pos = undef;
+while (my $employee = $group_4->next) {
+ $employee->move_to_group({group_id=>$to_group, group_id_2=>$to_group_2}, $to_pos);
+ $to_pos++;
+ $to_group = ($to_group % 3) + 1;
+ $to_group_2_base++;
+ $to_group_2 = (ceil($to_group_2_base/3.0) %3) +1
+}
+foreach my $group_id (1..4) {
+ foreach my $group_id_2 (1..4) {
+ my $group_employees = $employees->search({group_id=>$group_id,group_id_2=>$group_id_2});
+ $group_employees->all();
+ ok( check_rs($group_employees), "group positions after move_to_group" );
+ }
+}
+
+$employees->delete();
+foreach my $group_id (1..4) {
+ foreach my $group_id_2 (1..4) {
+ foreach (1..4) {
+ $employees->create({ name=>'temp', group_id=>$group_id, group_id_2=>$group_id_2 });
+ }
+ }
+}
+$employees = $employees->search(undef,{order_by=>'group_id,group_id_2,position'});
+
+$employee = $employees->search({group_id=>4, group_id_2=>1})->first;
+$employee->group_id(1);
+$employee->update;
+ok(
+ check_rs($employees->search_rs({group_id=>4, group_id_2=>1}))
+ && check_rs($employees->search_rs({group_id=>1, group_id_2=>1})),
+ "overloaded multicol update 1"
+);
+
+$employee = $employees->search({group_id=>4, group_id_2=>1})->first;
+$employee->update({group_id=>2});
+ok( check_rs($employees->search_rs({group_id=>4, group_id_2=>1}))
+ && check_rs($employees->search_rs({group_id=>2, group_id_2=>1})),
+ "overloaded multicol update 2"
+);
+
+$employee = $employees->search({group_id=>3, group_id_2=>1})->first;
+$employee->group_id(1);
+$employee->group_id_2(3);
+$employee->update();
+ok( check_rs($employees->search_rs({group_id=>3, group_id_2=>1}))
+ && check_rs($employees->search_rs({group_id=>1, group_id_2=>3})),
+ "overloaded multicol update 3"
+);
+
+$employee = $employees->search({group_id=>3, group_id_2=>1})->first;
+$employee->update({group_id=>2, group_id_2=>3});
+ok( check_rs($employees->search_rs({group_id=>3, group_id_2=>1}))
+ && check_rs($employees->search_rs({group_id=>2, group_id_2=>3})),
+ "overloaded multicol update 4"
+);
+
+$employee = $employees->search({group_id=>3, group_id_2=>2})->first;
+$employee->update({group_id=>2, group_id_2=>4, position=>2});
+ok( check_rs($employees->search_rs({group_id=>3, group_id_2=>2}))
+ && check_rs($employees->search_rs({group_id=>2, group_id_2=>4})),
+ "overloaded multicol update 5"
+);
+
sub hammer_rs {
my $rs = shift;
my $employee;