X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FLinkedRecords.pm;fp=SL%2FDB%2FHelper%2FLinkedRecords.pm;h=33248bfaee9ec9d41f11da6a5b7d468cc59db6c9;hb=f5701b02123494f8dacbea8c5de00cf7a6edba70;hp=d072a55c7546cdf6c9038e7e7ff77bab19c18d5c;hpb=7c667c9093239a08aec7e9ab0958f6f721a8d6af;p=kivitendo-erp.git diff --git a/SL/DB/Helper/LinkedRecords.pm b/SL/DB/Helper/LinkedRecords.pm index d072a55c7..33248bfae 100644 --- a/SL/DB/Helper/LinkedRecords.pm +++ b/SL/DB/Helper/LinkedRecords.pm @@ -30,7 +30,19 @@ sub _linked_records_implementation { my $self = shift; my %params = @_; - my $wanted = $params{direction} || croak("Missing parameter `direction'"); + my $wanted = $params{direction}; + + if (!$wanted) { + if ($params{to} && $params{from}) { + $wanted = 'both'; + } elsif ($params{to}) { + $wanted = 'to'; + } elsif ($params{from}) { + $wanted = 'from'; + } else { + $wanted = 'both'; + } + } if ($wanted eq 'both') { my $both = delete($params{both}); @@ -132,7 +144,7 @@ sub link_to_record { ); my $link = SL::DB::Manager::RecordLink->find_by(and => [ %data ]); - push @links, $link ? $link : SL::DB::RecordLink->new(%data)->save unless $link; + push @links, $link ? $link : SL::DB::RecordLink->new(%data)->save; } return wantarray ? @links : $links[0]; @@ -240,32 +252,26 @@ SYNOPSIS use SL::DB::Helper::LinkedRecords; # later in consumer code - # retrieve all links - my @linked_objects = $order->linked_records( - direction => 'both', - ); + # retrieve all links in both directions + my @linked_objects = $order->linked_records; # only links to Invoices my @linked_objects = $order->linked_records( - direction => 'to', to => 'Invoice', ); # more than one target my @linked_objects = $order->linked_records( - direction => 'to', to => [ 'Invoice', 'Order' ], ); # more than one direction my @linked_objects = $order->linked_records( - direction => 'both', both => 'Invoice', ); # more than one direction and different targets my @linked_objects = $order->linked_records( - direction => 'both', to => 'Invoice', from => 'Order', ); @@ -277,6 +283,13 @@ SYNOPSIS via => 'DeliveryOrder', ); + # limit direction when further params contain additional keys + my %params = (to => 'Invoice', from => 'Order'); + my @linked_objects = $order->linked_records( + direction => 'to', + %params, + ); + # add a new link $order->link_to_record($invoice); $order->link_to_record($purchase_order, bidirectional => 1); @@ -288,11 +301,13 @@ SYNOPSIS =item C -Retrieves records linked from or to C<$self> via the table C. The -mandatory parameter C (either C, C or C) determines -whether the function retrieves records that link to C<$self> (for C -= C) or that are linked from C<$self> (for C = C). For -C all records linked from or to C<$self> are returned. +Retrieves records linked from or to C<$self> via the table C. + +The optional parameter C (either C, C or C) +determines whether the function retrieves records that link to C<$self> (for +C = C) or that are linked from C<$self> (for C = +C). For C all records linked from or to C<$self> are +returned. The optional parameter C or C (same as C) contains the package names of Rose models for table limitation (the prefix C is @@ -300,6 +315,10 @@ optional). It can be a single model name as a single scalar or multiple model names in an array reference in which case all links matching any of the model names will be returned. +If no parameter C is given, but any of C, C or C, +then C is infered accordingly. If neither are given, C is +set to C. + The optional parameter C can be used to retrieve all documents that may have intermediate documents inbetween. It is an array reference of Rose package names for the models that may be intermediate link targets. One example is