[Dbix-class] Re: Easy update for has_many?
Matt S Trout
dbix-class at trout.me.uk
Wed Sep 12 17:06:49 GMT 2007
On Thu, Sep 06, 2007 at 02:10:32AM +0200, A. Pagaltzis wrote:
> * 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.
I think we really need to look at providing set_foos and remove_from_foos
methods for has_many as well as many_many and trying to get them to share
common code. They are after all both resultset-based.
--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director Want a managed development or deployment platform?
Shadowcat Systems Ltd. Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/
More information about the DBIx-Class
mailing list