Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

DBIx::Class recursive fetch/delete?

by stonecolddevin (Parson)
on Apr 24, 2007 at 05:58 UTC ( #611657=perlquestion: print w/replies, xml ) Need Help??

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('', { 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 '' %] <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 '' %]

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

thanks in advance,



Replies are listed 'Best First'.
Re: DBIx::Class recursive fetch/delete?
by Ojosh!ro (Beadle) on Apr 24, 2007 at 08:22 UTC
    Hi, I don't know whether thishelps your case

    Thanks, you sent me reading into another direction. While I was following the Class::DBI-link on the DBIx::Class documentation-site I found this: Music::CD->search(year => 1980, title => 'Greatest %')->delete_all;
    I see DBIx::Class should be compatible.
    Hope it helps you.

    if( exists $aeons{strange} ){ die $death unless ( $death%2 ) }

      That doesn't take into account that I need to pass multiple photo_ids.

      I'm wondering if I need to pass them as an array(ref) or some such?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://611657]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2021-06-13 09:18 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (54 votes). Check out past polls.