Merge branch 'b-3.6.1' into mebil
[kivitendo-erp.git] / SL / DB / Helper / Util.pm
1 package SL::DB::Helper::Util;
2
3 use strict;
4
5 use Rose::DB::Object::Util;
6
7 use parent qw(Exporter);
8 our @EXPORT_OK = qw(is_unique);
9
10 #
11 # Public functions not exported by default
12 #
13
14 sub is_unique {
15   my ($self, @columns) = @_;
16
17   my @filter =  map { ($_ => $self->$_) } @columns;
18   if (Rose::DB::Object::Util::is_in_db($self)) {
19     push @filter, map { ("!${_}" => $self->$_ )} $self->meta->primary_key;
20   }
21
22   return !$self->_get_manager_class->get_first(where => [ and => \@filter ]);
23 }
24
25 1;
26
27 __END__
28
29 =pod
30
31 =encoding utf8
32
33 =head1 NAME
34
35 SL::DB::Helper::Util - Helper functions for Rose::DB::Object instances
36
37 =head1 SYNOPSIS
38
39   package SL::DB::AuthUser;
40
41   use SL::DB::Helper::Util;
42
43   sub validate {
44     ...
45     if (!SL::DB::Helper::Util::is_unique($self, 'login')) {
46       push @errors, "Login not unique";
47     }
48   }
49
50 =head1 OVERVIEW
51
52 This is a collection of assorted helper and utility functions for
53 Rose::DB::Object instances that don't require full-blown mixin helpers
54 like L<SL::DB::ActsAsList>. The module does not export any function by
55 default, but all of the public ones can be requested in the usual
56 way.
57
58 Each function can be called either as a fully qualified name with the
59 object instance as the first argument or (if the function is imported)
60 as an instance method on the object instance.
61
62 =head1 FUNCTIONS
63
64 =over 4
65
66 =item C<is_unique @columns>
67
68 Returns trueish if C<$self> is unique in its table regarding the
69 columns C<@columns>. What it does is look for existing records in the
70 database whose stored column values match C<$self>'s current values
71 for these columns. If C<$self> already exists in the database then
72 that row is not considered during the search.
73
74 =back
75
76 =head1 BUGS
77
78 Nothing here yet.
79
80 =head1 AUTHOR
81
82 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
83
84 =cut