[Dbix-class] Re: Easy update for has_many?

A. Pagaltzis pagaltzis at gmx.de
Thu Sep 6 01:10:32 GMT 2007


* Jason Kohles <email at jasonkohles.com> [2007-08-22 16:35]:
> There is probably a cleaner way, but this is how I'm doing it
> at the moment...

There certainly is.

    sub update_profiles {
        my ( $self, @profiles ) = @_;
        my %new_profile = map { ( $_, $_ ) } @profiles;

        # first kill existing profile links that don't show up in the new list
        $self->profiles->search( { profile_id => { -not_in => @profile } } )->delete;

        # then disregard profile links that are still in the database
        delete @new_profile{ map { $_->profile_id } $self->profiles };

        # and finally create the ones that aren't yet
        $self->add_to_profiles( { profile_id => $_ } ) for values %new_profile;

        return $self;
    }

Untested.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the DBIx-Class mailing list