From f3345acc15cb39c02047ff24c626f280f638c4e8 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 12 Jan 2011 16:01:36 +0100 Subject: [PATCH] Hilfsmethode zum Linken von einem Record auf ein anderes --- SL/DB/Helper/LinkedRecords.pm | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/SL/DB/Helper/LinkedRecords.pm b/SL/DB/Helper/LinkedRecords.pm index 6dc246d15..a1183d75f 100644 --- a/SL/DB/Helper/LinkedRecords.pm +++ b/SL/DB/Helper/LinkedRecords.pm @@ -2,7 +2,7 @@ package SL::DB::Helpers::LinkedRecords; require Exporter; our @ISA = qw(Exporter); -our @EXPORT = qw(linked_records); +our @EXPORT = qw(linked_records link_to_record); use Carp; @@ -42,6 +42,23 @@ sub linked_records { return $records; } +sub link_to_record { + my $self = shift; + my $other = shift; + + 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 $link = SL::DB::Manager::RecordLink->find_by(and => [ %params ]); + return $link ? $link : SL::DB::RecordLink->new(%params)->save; +} + 1; __END__ @@ -82,6 +99,15 @@ created today: Returns an array reference. +=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. + =back =head1 BUGS -- 2.20.1