From 38043ff08cd016cfcbe8c8292541fb553b7edb66 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 12 Jan 2011 16:03:57 +0100 Subject: [PATCH] Optionale bidirektionale Verlinkung in LinkHelper::link_to_record Conflicts: SL/DB/Helper/LinkedRecords.pm --- SL/DB/Helper/LinkedRecords.pm | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/SL/DB/Helper/LinkedRecords.pm b/SL/DB/Helper/LinkedRecords.pm index 9dbee05ec..8784811a7 100644 --- a/SL/DB/Helper/LinkedRecords.pm +++ b/SL/DB/Helper/LinkedRecords.pm @@ -65,18 +65,27 @@ sub linked_records { sub link_to_record { my $self = shift; my $other = shift; + my %params = @_; croak "self has no id" unless $self->id; croak "other has no id" unless $other->id; - my %params = ( from_table => SL::DB::Helpers::Mappings::get_table_for_package(ref($self)), - from_id => $self->id, - to_table => SL::DB::Helpers::Mappings::get_table_for_package(ref($other)), - to_id => $other->id, - ); + my @directions = ([ 'from', 'to' ]); + push @directions, [ 'to', 'from' ] if $params{bidirectional}; + my @links; - my $link = SL::DB::Manager::RecordLink->find_by(and => [ %params ]); - return $link ? $link : SL::DB::RecordLink->new(%params)->save; + foreach my $direction (@directions) { + my %params = ( $direction->[0] . "_table" => SL::DB::Helper::Mappings::get_table_for_package(ref($self)), + $direction->[0] . "_id" => $self->id, + $direction->[1] . "_table" => SL::DB::Helper::Mappings::get_table_for_package(ref($other)), + $direction->[1] . "_id" => $other->id, + ); + + my $link = SL::DB::Manager::RecordLink->find_by(and => [ %params ]); + push @links, $link ? $link : SL::DB::RecordLink->new(%params)->save unless $link; + } + + return wantarray ? @links : $links[0]; } sub linked_records_sorted { @@ -201,14 +210,20 @@ created today: Returns an array reference. -=item C +=item C Will create an entry in the table C with the C side being C<$self> and the C side being C<$record>. Will only insert a new entry if such a link does not already exist. -Returns either the existing link or the newly created one as an -instance of C. +If C<$params{bidirectional}> is trueish then another link will be +created with the roles of C and C reversed. This link will +also only be created if it doesn't exist already. + +In scalar contenxt returns either the existing link or the newly +created one as an instance of C. In array context +it returns an array of links (one entry if C<$params{bidirectional}> +is falsish and two entries if it is trueish). =item C -- 2.20.1