1 package SL::DB::Helpers::LinkedRecords;
4 our @ISA = qw(Exporter);
5 our @EXPORT = qw(linked_records);
9 use SL::DB::Helpers::Mappings;
10 use SL::DB::RecordLink;
16 my $wanted = $params{direction} || croak("Missing parameter `direction'");
17 my $myself = $wanted eq 'from' ? 'to' : $wanted eq 'to' ? 'from' : croak("Invalid parameter `direction'");
19 my $my_table = SL::DB::Helpers::Mappings::get_table_for_package(ref($self));
21 my @query = ( "${myself}_table" => $my_table,
22 "${myself}_id" => $self->id );
24 if ($params{$wanted}) {
25 my $wanted_table = SL::DB::Helpers::Mappings::get_table_for_package($params{$wanted}) || croak("Invalid parameter `${wanted}'");
26 push @query, ("${wanted}_table" => $wanted_table);
29 my $links = SL::DB::Manager::RecordLink->get_all(query => [ and => \@query ]);
31 my $sub_wanted_table = "${wanted}_table";
32 my $sub_wanted_id = "${wanted}_id";
35 @query = ref($params{query}) eq 'ARRAY' ? @{ $params{query} } : ();
37 foreach my $link (@{ $links }) {
38 my $class = SL::DB::Helpers::Mappings::get_manager_package_for_table($link->$sub_wanted_table);
39 push @{ $records }, @{ $class->get_all(query => [ id => $link->$sub_wanted_id, @query ]) };
53 SL::DB::Helpers::LinkedRecords - Mixin for retrieving linked records via the table C<record_links>
59 =item C<linked_records %params>
61 Retrieves records linked from or to C<$self> via the table
62 C<record_links>. The mandatory parameter C<direction> (either C<from>
63 or C<to>) determines whether the function retrieves records that link
64 to C<$self> (for C<direction> = C<to>) or that are linked from
65 C<$self> (for C<direction> = C<from>).
67 The optional parameter C<from> or C<to> (same as C<direction>)
68 contains the package name of a Rose model for table limitation. If you
69 only need invoices created from an order C<$order> then the call could
72 my $invoices = $order->linked_records(direction => 'to',
73 to => 'SL::DB::Invoice');
75 The optional parameter C<query> can be used to limit the records
76 returned. The following call limits the earlier example to invoices
79 my $invoices = $order->linked_records(direction => 'to',
80 to => 'SL::DB::Invoice',
81 query => [ transdate => DateTime->today_local ]);
83 Returns an array reference.
93 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>