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.

In reply to DBIx::Class recursive fetch/delete? by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.