doubt about has_many and update

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

doubt about has_many and update

Luca Ferrari
Hi all,
I've some confusion about the has_many relationship and how to quickly
update a set of value thru a join table.
I've a class Ticket with has_many Worker, and a join table.
The following works for adding a worker to a ticket:

 for my $current_worker (@workers){
            $ticket->add_to_tickets_workers( { pk_ticket =>
$ticket->pk,
                                               pk_worker =>
$current_worker->pk } ) if ( grep { $current_worker->pk == $_ }
@selected );


However I would like to have a set_tickets_workers behavior, so that I
can pass the array 8ref) of objects I want to substitute on the join
table.
Is there a way to do it or do I have to manually delete join rows (in
such case, how)?

Thanks,
Luca

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: doubt about has_many and update

Luca Ferrari
So far, what I've done is to "emulate" the set method as follows:

sub set_workers{
    my ($self, $workers) = @_;

    for my $current_worker ( $self->tickets_workers->all() ){
        $current_worker->delete() if ( ! grep { $_->pk ==
$current_worker->pk } @{ $workers } );
    }

    for my $worker ( @$workers ){
        $self->add_to_tickets_workers( { pk_ticket => $self->pk,
pk_worker => $worker->pk } );
    }
}

and therefore delete each joined object and then add it again. I can
elaborate  a little more on this to trim the queries hitting the
database, but for small result sets it can work.
Any suggestion on a smarter solution?

Thanks,
Luca

On Mon, Feb 2, 2015 at 4:11 PM, Luca Ferrari <[hidden email]> wrote:

> Hi all,
> I've some confusion about the has_many relationship and how to quickly
> update a set of value thru a join table.
> I've a class Ticket with has_many Worker, and a join table.
> The following works for adding a worker to a ticket:
>
>  for my $current_worker (@workers){
>             $ticket->add_to_tickets_workers( { pk_ticket =>
> $ticket->pk,
>                                                pk_worker =>
> $current_worker->pk } ) if ( grep { $current_worker->pk == $_ }
> @selected );
>
>
> However I would like to have a set_tickets_workers behavior, so that I
> can pass the array 8ref) of objects I want to substitute on the join
> table.
> Is there a way to do it or do I have to manually delete join rows (in
> such case, how)?
>
> Thanks,
> Luca

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Loading...