SL::DB::Helper::Manager: mehr Dokumentation
[kivitendo-erp.git] / SL / DB / Helper / Manager.pm
1 package SL::DB::Helper::Manager;
2
3 use strict;
4
5 use Carp;
6
7 use Rose::DB::Object::Manager;
8 use base qw(Rose::DB::Object::Manager);
9
10 sub make_manager_methods {
11   my $class  = shift;
12   my @params = scalar(@_) ? @_ : qw(all);
13   return $class->SUPER::make_manager_methods(@params);
14 }
15
16 sub find_by {
17   my $class = shift;
18
19   return if !@_;
20   return $class->get_all(query => [ @_ ], limit => 1)->[0];
21 }
22
23 sub find_by_or_create {
24   my $class = shift;
25
26   my $found;
27   eval {
28     $found = $class->find_by(@_);
29     1;
30   } or die $@;
31   return defined $found ? $found : $class->object_class->new;
32 }
33
34 sub get_first {
35   shift->get_all(
36     @_,
37     limit => 1,
38   )->[0];
39 }
40
41 sub cache_all {
42   my $manager_class =  shift;
43   my $class         =  $manager_class;
44   $class            =~ s{Manager::}{};
45
46   croak "Caching can only be used with classes with exactly one primary key column" if 1 != scalar(@{ $class->meta->primary_key_columns });
47
48   my $all_have_been_cached =  $::request->cache("::SL::DB::Manager::cache_all");
49   return if $all_have_been_cached->{$class};
50
51   $all_have_been_cached->{$class} = 1;
52
53   my $item_cache                  = $::request->cache("::SL::DB::Object::object_cache::${class}");
54   my $primary_key                 = $class->meta->primary_key_columns->[0]->name;
55   my $objects                     = $class->_get_manager_class->get_all;
56
57   $item_cache->{$_->$primary_key} = $_ for @{ $objects};
58 }
59
60 1;
61 __END__
62
63 =pod
64
65 =encoding utf8
66
67 =head1 NAME
68
69 SL::DB::Helper::Manager - Base class & helper functions for all Rose manager classes
70
71 =head1 FUNCTIONS
72
73 =over 4
74
75 =item C<cache_all>
76
77 Pre-caches all items from a table. Use this is you expect to need all
78 items from a table. You can retrieve them later with the
79 C<load_cached> function from the corresponding Rose DB object class.
80
81 For example, if you expect to need all unit objects, you can use
82 C<SL::DB::Manager::Unit-E<gt>cache_all> before you start the actual
83 work. Later you can use C<SL::DB::Unit-E<gt>load_cached> to retrieve
84 individual objects and be sure that they're already cached.
85
86 =item C<find_by @where>
87
88 Retrieves one item from the corresponding table matching the
89 conditions given in C<@where>.
90
91 This is shorthand for the following call:
92
93     SL::DB::Manager::SomeClass->get_all(where => [ … ], limit => 1)
94
95 =item C<find_by_or_create @where>
96
97 This calls L</find_by> with C<@where> and returns its result if one is
98 found.
99
100 If none is found, a new instance of the corresponding DB object class
101 is created and returned. Such a new object is not inserted into the
102 database automatically.
103
104 =item C<get_first @args>
105
106 Retrieves the first item from the database by calling C<get_all> with
107 a limit of 1. The C<@args> are passed through to C<get_all> allowing
108 for arbitrary filters and sort options to be applied.
109
110 =item C<make_manager_methods [@method_list]>
111
112 Calls Rose's C<make_manager_methods> with the C<@method_list> or
113 C<all> if no methods are given.
114
115 =back
116
117 =head1 BUGS
118
119 Nothing here yet.
120
121 =head1 AUTHOR
122
123 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
124
125 =cut