http://www.perlmonks.org?node_id=611657

stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:

Hey gang,

I'm attempting to delete multiple records via a web interface using check boxes. I can get the first step done, retrieving the record(s) that is to be deleted (photo paths in this case) and displaying it, however beyond that I'm having trouble figuring out how to delete all the records selected. I'm thinking I need a recursive delete, but how using DBIx::Class?

Here's my code:

sub delete_photos { my $self = shift; my $q = $self->query; my $other_photos = $schema->resultset('Photos')->search( { user_id => $q->url_param('user'), } ); my @photos_to_delete = (); my @photo_ids = $q->param('delete'); while ( my $photos = $other_photos->next ) { for ( @photo_ids ) { if ( $photos->photo_id == $_ ) { push @photos_to_delete, { photo_path => $photos->photo +_path, photo_id => $photos->photo_id }; } } } unless ( $q->param('is_sure') ) { return $self->tt_process('delete_photo_confirm.tt', { title => 'Photo delete confirm', c => $q, photos => \@photos_to_delete, } ); } else { $schema->resultset('Photos')->search( { user_id => $q->url_param('user'), photo_id => @photo_ids, # attempt at recursive delete + } )->delete or die $!; } unlink $_ for @photos_to_delete or die $!; return "Photos deleted!"; }

The template:

[% INCLUDE 'header.tt' %] <body> <h2>Are you sure?</h2> <p>You have selected these photos to delete:</p> <form action="" method="post"> <input type="hidden" name="p" value="delete" /> <input type="hidden" name="is_sure" value="1" /> [% FOREACH p = photos %] <p><img src="[% p.photo_path %]" /> <input type="checkbox" nam +e="delete_photo" value="[% p.photo_id %]" /></p> [% END %] <input type="submit" name="Delete These Photos" /> </form> [% INCLUDE 'footer.tt' %]

Apologies if this is a less than intelligent node, i'm a bit sleepy :-P

thanks in advance,

-dhoss

meh.