[Dbix-class] doubt about has_many and update
Luca Ferrari
fluca1978 at infinito.it
Mon Feb 2 16:53:53 GMT 2015
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 <fluca1978 at infinito.it> 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
More information about the DBIx-Class
mailing list