1 package SL::DB::Object;
 
   6 use List::MoreUtils qw(any);
 
   9 use SL::DB::Helper::Attr;
 
  10 use SL::DB::Helper::Metadata;
 
  11 use SL::DB::Helper::Manager;
 
  13 use base qw(Rose::DB::Object);
 
  17   my $self  = $class->SUPER::new();
 
  19   $self->_assign_attributes(@_) if $self;
 
  25   my $class_or_self = shift;
 
  26   my $class         = ref($class_or_self) || $class_or_self;
 
  27   my $type          = $class =~ m/::Auth/ ? 'LXOFFICE_AUTH' : 'LXOFFICE';
 
  29   return SL::DB::create(undef, $type);
 
  33   return 'SL::DB::Helper::Metadata';
 
  36 sub _get_manager_class {
 
  37   my $class_or_self = shift;
 
  38   my $class         = ref($class_or_self) || $class_or_self;
 
  40   return $class->meta->convention_manager->auto_manager_class_name($class);
 
  43 my %text_column_types = (text => 1, char => 1, varchar => 1);
 
  45 sub assign_attributes {
 
  49   my $pk         = ref($self)->meta->primary_key;
 
  50   delete @attributes{$pk->column_names} if $pk;
 
  52   return $self->_assign_attributes(%attributes);
 
  55 sub _assign_attributes {
 
  59   my %types      = map { $_->name => $_->type } ref($self)->meta->columns;
 
  61   while (my ($attribute, $value) = each %attributes) {
 
  62     my $type = lc($types{$attribute} || 'text');
 
  63     $value   = $type eq 'boolean'                ? ($value ? 't' : 'f')
 
  64              : $text_column_types{$type}         ? $value
 
  65              : defined($value) && ($value eq '') ? undef
 
  67     $self->$attribute($value);
 
  73 sub update_attributes {
 
  76   $self->assign_attributes(@_)->save;
 
  84   return $self->$sub(@_);
 
  95 SL::DB::Object: Base class for all of our model classes
 
  99 This is the base class from which all other model classes are
 
 100 derived. It contains functionality and settings required for all model
 
 103 Several functions (e.g. C<make_manager_class>, C<init_db>) in this
 
 104 class are used for setting up the classes / base classes used for all
 
 105 model instances. They overwrite the functions from
 
 112 =item assign_attributes %attributes
 
 114 =item _assign_attributes %attributes
 
 116 Assigns all elements from C<%attributes> to the columns by calling
 
 117 their setter functions. The difference between the two functions is
 
 118 that C<assign_attributes> protects primary key columns while
 
 119 C<_assign_attributes> doesn't.
 
 121 Both functions handle values that are empty strings by replacing them
 
 122 with C<undef> for non-text columns. This allows the calling functions
 
 123 to use data from HTML forms as the input for C<assign_attributes>
 
 124 without having to remove empty strings themselves (think of
 
 125 e.g. select boxes with an empty option which should be turned into
 
 126 C<NULL> in the database).
 
 128 =item update_attributes %attributes
 
 130 Assigns the attributes from C<%attributes> by calling the
 
 131 C<assign_attributes> function and saves the object afterwards. Returns
 
 134 =item _get_manager_class
 
 136 Returns the manager package for the object or class that it is called
 
 137 on. Can be used from methods in this package for getting the actual
 
 140 =item C<call_sub $name, @args>
 
 142 Calls the sub C<$name> on C<$self> with the arguments C<@args> and
 
 143 returns its result. This is meant for situations in which the sub's
 
 144 name is a composite, e.g.
 
 146   my $chart_id = $buchungsgruppe->call_sub(($is_sales ? "income" : "expense") . "_accno_id_${taxzone_id}");
 
 152 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>