From: Sven Schöling Date: Fri, 30 Dec 2011 16:27:58 +0000 (+0100) Subject: Manager: find_by_or_create, sichere Variante ein Objekt zu finden oder zu autovivifien. X-Git-Tag: release-2.7.0beta1~111 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=a27dd3ae50d468c4e27ae6882ecca1fc7f3891ea;p=kivitendo-erp.git Manager: find_by_or_create, sichere Variante ein Objekt zu finden oder zu autovivifien. --- diff --git a/SL/DB/Helper/Manager.pm b/SL/DB/Helper/Manager.pm index 8ebc362e2..b4d66da42 100644 --- a/SL/DB/Helper/Manager.pm +++ b/SL/DB/Helper/Manager.pm @@ -18,6 +18,14 @@ sub find_by { return $class->get_all(query => [ @_ ], limit => 1)->[0]; } +sub find_by_or_create { + my $class = shift; + + my $found; + eval { $found = $class->find_by(@_); }; + return defined $found ? $found : $class->object_class->new; +} + sub get_first { shift->get_all( @_,