30 |
30 |
my $self = shift;
|
31 |
31 |
my %params = @_;
|
32 |
32 |
|
33 |
|
my $wanted = $params{direction} || croak("Missing parameter `direction'");
|
|
33 |
my $wanted = $params{direction};
|
|
34 |
|
|
35 |
if (!$wanted) {
|
|
36 |
if ($params{to} && $params{from}) {
|
|
37 |
$wanted = 'both';
|
|
38 |
} elsif ($params{to}) {
|
|
39 |
$wanted = 'to';
|
|
40 |
} elsif ($params{from}) {
|
|
41 |
$wanted = 'from';
|
|
42 |
} else {
|
|
43 |
$wanted = 'both';
|
|
44 |
}
|
|
45 |
}
|
34 |
46 |
|
35 |
47 |
if ($wanted eq 'both') {
|
36 |
48 |
my $both = delete($params{both});
|
... | ... | |
132 |
144 |
);
|
133 |
145 |
|
134 |
146 |
my $link = SL::DB::Manager::RecordLink->find_by(and => [ %data ]);
|
135 |
|
push @links, $link ? $link : SL::DB::RecordLink->new(%data)->save unless $link;
|
|
147 |
push @links, $link ? $link : SL::DB::RecordLink->new(%data)->save;
|
136 |
148 |
}
|
137 |
149 |
|
138 |
150 |
return wantarray ? @links : $links[0];
|
... | ... | |
240 |
252 |
use SL::DB::Helper::LinkedRecords;
|
241 |
253 |
|
242 |
254 |
# later in consumer code
|
243 |
|
# retrieve all links
|
244 |
|
my @linked_objects = $order->linked_records(
|
245 |
|
direction => 'both',
|
246 |
|
);
|
|
255 |
# retrieve all links in both directions
|
|
256 |
my @linked_objects = $order->linked_records;
|
247 |
257 |
|
248 |
258 |
# only links to Invoices
|
249 |
259 |
my @linked_objects = $order->linked_records(
|
250 |
|
direction => 'to',
|
251 |
260 |
to => 'Invoice',
|
252 |
261 |
);
|
253 |
262 |
|
254 |
263 |
# more than one target
|
255 |
264 |
my @linked_objects = $order->linked_records(
|
256 |
|
direction => 'to',
|
257 |
265 |
to => [ 'Invoice', 'Order' ],
|
258 |
266 |
);
|
259 |
267 |
|
260 |
268 |
# more than one direction
|
261 |
269 |
my @linked_objects = $order->linked_records(
|
262 |
|
direction => 'both',
|
263 |
270 |
both => 'Invoice',
|
264 |
271 |
);
|
265 |
272 |
|
266 |
273 |
# more than one direction and different targets
|
267 |
274 |
my @linked_objects = $order->linked_records(
|
268 |
|
direction => 'both',
|
269 |
275 |
to => 'Invoice',
|
270 |
276 |
from => 'Order',
|
271 |
277 |
);
|
... | ... | |
277 |
283 |
via => 'DeliveryOrder',
|
278 |
284 |
);
|
279 |
285 |
|
|
286 |
# limit direction when further params contain additional keys
|
|
287 |
my %params = (to => 'Invoice', from => 'Order');
|
|
288 |
my @linked_objects = $order->linked_records(
|
|
289 |
direction => 'to',
|
|
290 |
%params,
|
|
291 |
);
|
|
292 |
|
280 |
293 |
# add a new link
|
281 |
294 |
$order->link_to_record($invoice);
|
282 |
295 |
$order->link_to_record($purchase_order, bidirectional => 1);
|
... | ... | |
288 |
301 |
|
289 |
302 |
=item C<linked_records %params>
|
290 |
303 |
|
291 |
|
Retrieves records linked from or to C<$self> via the table C<record_links>. The
|
292 |
|
mandatory parameter C<direction> (either C<from>, C<to> or C<both>) determines
|
293 |
|
whether the function retrieves records that link to C<$self> (for C<direction>
|
294 |
|
= C<to>) or that are linked from C<$self> (for C<direction> = C<from>). For
|
295 |
|
C<direction = both> all records linked from or to C<$self> are returned.
|
|
304 |
Retrieves records linked from or to C<$self> via the table C<record_links>.
|
|
305 |
|
|
306 |
The optional parameter C<direction> (either C<from>, C<to> or C<both>)
|
|
307 |
determines whether the function retrieves records that link to C<$self> (for
|
|
308 |
C<direction> = C<to>) or that are linked from C<$self> (for C<direction> =
|
|
309 |
C<from>). For C<direction = both> all records linked from or to C<$self> are
|
|
310 |
returned.
|
296 |
311 |
|
297 |
312 |
The optional parameter C<from> or C<to> (same as C<direction>) contains the
|
298 |
313 |
package names of Rose models for table limitation (the prefix C<SL::DB::> is
|
... | ... | |
300 |
315 |
names in an array reference in which case all links matching any of the model
|
301 |
316 |
names will be returned.
|
302 |
317 |
|
|
318 |
If no parameter C<direction> is given, but any of C<to>, C<from> or C<both>,
|
|
319 |
then C<direction> is infered accordingly. If neither are given, C<direction> is
|
|
320 |
set to C<both>.
|
|
321 |
|
303 |
322 |
The optional parameter C<via> can be used to retrieve all documents that may
|
304 |
323 |
have intermediate documents inbetween. It is an array reference of Rose package
|
305 |
324 |
names for the models that may be intermediate link targets. One example is
|
LinkedRecords: bidi Verhalten den docs angepasst und direction Parameter optional gemacht