Revision 38043ff0
Von Moritz Bunkus vor fast 14 Jahren hinzugefügt
SL/DB/Helper/LinkedRecords.pm | ||
---|---|---|
65 | 65 |
sub link_to_record { |
66 | 66 |
my $self = shift; |
67 | 67 |
my $other = shift; |
68 |
my %params = @_; |
|
68 | 69 |
|
69 | 70 |
croak "self has no id" unless $self->id; |
70 | 71 |
croak "other has no id" unless $other->id; |
71 | 72 |
|
72 |
my %params = ( from_table => SL::DB::Helpers::Mappings::get_table_for_package(ref($self)), |
|
73 |
from_id => $self->id, |
|
74 |
to_table => SL::DB::Helpers::Mappings::get_table_for_package(ref($other)), |
|
75 |
to_id => $other->id, |
|
76 |
); |
|
73 |
my @directions = ([ 'from', 'to' ]); |
|
74 |
push @directions, [ 'to', 'from' ] if $params{bidirectional}; |
|
75 |
my @links; |
|
77 | 76 |
|
78 |
my $link = SL::DB::Manager::RecordLink->find_by(and => [ %params ]); |
|
79 |
return $link ? $link : SL::DB::RecordLink->new(%params)->save; |
|
77 |
foreach my $direction (@directions) { |
|
78 |
my %params = ( $direction->[0] . "_table" => SL::DB::Helper::Mappings::get_table_for_package(ref($self)), |
|
79 |
$direction->[0] . "_id" => $self->id, |
|
80 |
$direction->[1] . "_table" => SL::DB::Helper::Mappings::get_table_for_package(ref($other)), |
|
81 |
$direction->[1] . "_id" => $other->id, |
|
82 |
); |
|
83 |
|
|
84 |
my $link = SL::DB::Manager::RecordLink->find_by(and => [ %params ]); |
|
85 |
push @links, $link ? $link : SL::DB::RecordLink->new(%params)->save unless $link; |
|
86 |
} |
|
87 |
|
|
88 |
return wantarray ? @links : $links[0]; |
|
80 | 89 |
} |
81 | 90 |
|
82 | 91 |
sub linked_records_sorted { |
... | ... | |
201 | 210 |
|
202 | 211 |
Returns an array reference. |
203 | 212 |
|
204 |
=item C<link_to_record $record> |
|
213 |
=item C<link_to_record $record, %params>
|
|
205 | 214 |
|
206 | 215 |
Will create an entry in the table C<record_links> with the C<from> |
207 | 216 |
side being C<$self> and the C<to> side being C<$record>. Will only |
208 | 217 |
insert a new entry if such a link does not already exist. |
209 | 218 |
|
210 |
Returns either the existing link or the newly created one as an |
|
211 |
instance of C<SL::DB::RecordLink>. |
|
219 |
If C<$params{bidirectional}> is trueish then another link will be |
|
220 |
created with the roles of C<from> and C<to> reversed. This link will |
|
221 |
also only be created if it doesn't exist already. |
|
222 |
|
|
223 |
In scalar contenxt returns either the existing link or the newly |
|
224 |
created one as an instance of C<SL::DB::RecordLink>. In array context |
|
225 |
it returns an array of links (one entry if C<$params{bidirectional}> |
|
226 |
is falsish and two entries if it is trueish). |
|
212 | 227 |
|
213 | 228 |
=item C<sort_linked_records $sort_by, $sort_dir, @records> |
214 | 229 |
|
Auch abrufbar als: Unified diff
Optionale bidirektionale Verlinkung in LinkHelper::link_to_record
Conflicts: