Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision f5701b02

Von Sven Schöling vor mehr als 10 Jahren hinzugefügt

  • ID f5701b02123494f8dacbea8c5de00cf7a6edba70
  • Vorgänger 7c667c90
  • Nachfolger adb2f4cd

LinkedRecords: bidi Verhalten den docs angepasst und direction Parameter optional gemacht

Unterschiede anzeigen:

SL/DB/Helper/LinkedRecords.pm
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
t/db_helper/record_links.t
1
use Test::More;
1
use Test::More tests => 43;
2 2

  
3 3
use strict;
4 4

  
......
113 113
is $link->to_id, $i->id, 'to_id';
114 114

  
115 115
# retrieve link
116
$links = $o1->linked_records;
117
is $links->[0]->id, $i->id, 'simple retrieve';
118

  
116 119
$links = $o1->linked_records(direction => 'to', to => 'Invoice');
117 120
is $links->[0]->id, $i->id, 'direct retrieve 1';
118 121

  
......
186 189
$links = $o2->linked_records(direction => 'both');
187 190
is @$links, 2, 'links without from/to get all';
188 191

  
192
# doc states you can limit with direction when giving excess params
193
$links = $d->linked_records(direction => 'to', to => 'Invoice', from => 'Order');
194
is $links->[0]->id, $i->id, 'direction to limit params  1';
195
is @$links, 1, 'direction to limit params 2';
196

  
189 197
# doc says there will be special values set... lets see
190 198
$links = $o1->linked_records(direction => 'to', to => 'Order');
191 199
is $links->[0]->{_record_link_direction}, 'to',  '_record_link_direction to';
......
196 204
is $links->[0]->{_record_link}->to_id, $o1->id,  '_record_link from';
197 205

  
198 206
# check if bidi returns an array of links
199
{ local $TODO = 'does not work as advertised';
200 207
my @links = $d->link_to_record($o2, bidirectional => 1);
201 208
is @links, 2, 'bidi returns array of links in array context';
202
}
203 209

  
204 210
#  via
205 211
$links = $o2->linked_records(direction => 'to', to => 'Invoice', via => 'DeliveryOrder');
......
272 278
$sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('date', 0, @records);
273 279
is_deeply $sorted, [$d, $o1, $i, $o2], 'sorting by transdate desc';
274 280

  
275
done_testing();
276

  
277 281
1;

Auch abrufbar als: Unified diff