1 package SL::DB::Object;
7 use List::MoreUtils qw(any);
10 use SL::DB::Helpers::Attr;
11 use SL::DB::Helpers::Metadata;
12 use SL::DB::Helpers::Manager;
14 use base qw(Rose::DB::Object);
18 my $self = $class->SUPER::new();
20 $self->_assign_attributes(@_) if $self;
26 my $class_or_self = shift;
27 my $class = ref($class_or_self) || $class_or_self;
28 my $type = 'LXOFFICE';
30 return SL::DB::create(undef, $type);
34 return 'SL::DB::Helpers::Metadata';
37 sub _get_manager_class {
38 my $class_or_self = shift;
39 my $class = ref($class_or_self) || $class_or_self;
41 return $class->meta->convention_manager->auto_manager_class_name($class);
44 Readonly my %text_column_types => (text => 1, char => 1, varchar => 1);
46 sub assign_attributes {
50 my $pk = ref($self)->meta->primary_key;
51 delete @attributes{$pk->column_names} if $pk;
53 return $self->_assign_attributes(%attributes);
56 sub _assign_attributes {
60 my %types = map { $_->name => $_->type } ref($self)->meta->columns;
62 while (my ($attribute, $value) = each %attributes) {
63 my $type = lc($types{$attribute} || 'text');
64 $value = $type eq 'boolean' ? ($value ? 't' : 'f')
65 : $text_column_types{$type} ? $value
67 $self->$attribute($value);
73 sub update_attributes {
76 $self->assign_attributes(@_)->save;
89 SL::DB::Object: Base class for all of our model classes
93 This is the base class from which all other model classes are
94 derived. It contains functionality and settings required for all model
97 Several functions (e.g. C<make_manager_class>, C<init_db>) in this
98 class are used for setting up the classes / base classes used for all
99 model instances. They overwrite the functions from
106 =item assign_attributes %attributes
108 =item _assign_attributes %attributes
110 Assigns all elements from C<%attributes> to the columns by calling
111 their setter functions. The difference between the two functions is
112 that C<assign_attributes> protects primary key columns while
113 C<_assign_attributes> doesn't.
115 Both functions handle values that are empty strings by replacing them
116 with C<undef> for non-text columns. This allows the calling functions
117 to use data from HTML forms as the input for C<assign_attributes>
118 without having to remove empty strings themselves (think of
119 e.g. select boxes with an empty option which should be turned into
120 C<NULL> in the database).
122 =item update_attributes %attributes
124 Assigns the attributes from C<%attributes> by calling the
125 C<assign_attributes> function and saves the object afterwards. Returns
128 =item _get_manager_class
130 Returns the manager package for the object or class that it is called
131 on. Can be used from methods in this package for getting the actual
138 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>