1 package SL::DB::Helper::Manager;
 
   7 use Rose::DB::Object::Manager;
 
   8 use base qw(Rose::DB::Object::Manager);
 
  10 sub make_manager_methods {
 
  12   my @params = scalar(@_) ? @_ : qw(all);
 
  13   return $class->SUPER::make_manager_methods(@params);
 
  20   return $class->get_all(query => [ @_ ], limit => 1)->[0];
 
  23 sub find_by_or_create {
 
  28     $found = $class->find_by(@_);
 
  31   return defined $found ? $found : $class->object_class->new;
 
  42   my $manager_class =  shift;
 
  43   my $class         =  $manager_class;
 
  44   $class            =~ s{Manager::}{};
 
  46   croak "Caching can only be used with classes with exactly one primary key column" if 1 != scalar(@{ $class->meta->primary_key_columns });
 
  48   my $all_have_been_cached =  $::request->cache("::SL::DB::Manager::cache_all");
 
  49   return if $all_have_been_cached->{$class};
 
  51   $all_have_been_cached->{$class} = 1;
 
  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;
 
  57   $item_cache->{$_->$primary_key} = $_ for @{ $objects};
 
  69 SL::DB::Helper::Manager - Base class & helper functions for all Rose manager classes
 
  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.
 
  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.
 
  86 =item C<find_by @where>
 
  88 Retrieves one item from the corresponding table matching the
 
  89 conditions given in C<@where>.
 
  91 This is shorthand for the following call:
 
  93     SL::DB::Manager::SomeClass->get_all(where => [ … ], limit => 1)
 
  95 =item C<find_by_or_create @where>
 
  97 This calls L</find_by> with C<@where> and returns its result if one is
 
 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.
 
 104 =item C<get_first @args>
 
 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.
 
 110 =item C<make_manager_methods [@method_list]>
 
 112 Calls Rose's C<make_manager_methods> with the C<@method_list> or
 
 113 C<all> if no methods are given.
 
 123 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>