From: Moritz Bunkus Date: Wed, 12 Jan 2011 15:01:36 +0000 (+0100) Subject: Hilfsmethode zum Linken von einem Record auf ein anderes X-Git-Tag: release-2.6.3~61^2~7^2~1^2~2^2~94 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=f3345acc15cb39c02047ff24c626f280f638c4e8;p=kivitendo-erp.git Hilfsmethode zum Linken von einem Record auf ein anderes --- 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