From: Matt S Trout Date: Sun, 8 Jan 2006 04:08:40 +0000 (+0000) Subject: moved find_or_create onto ResultSet X-Git-Tag: v0.05005~118^2~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c2b15eccdc6d7a3a07bcb413e3c15ea907cfa643;p=dbsrgits%2FDBIx-Class.git moved find_or_create onto ResultSet --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 1e5ba7f..824c51e 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -424,6 +424,22 @@ sub create { return $self->new_result($attrs)->insert; } +=head2 find_or_create(\%vals) + + $class->find_or_create({ key => $val, ... }); + +Searches for a record matching the search condition; if it doesn't find one, +creates one and returns that instead. + +=cut + +sub find_or_create { + my $self = shift; + my $hash = ref $_[0] eq "HASH" ? shift: {@_}; + my $exists = $self->find($hash); + return defined($exists) ? $exists : $self->create($hash); +} + =head1 ATTRIBUTES The resultset takes various attributes that modify its behavior. diff --git a/lib/DBIx/Class/ResultSetInstance.pm b/lib/DBIx/Class/ResultSetInstance.pm index 40dd74a..0476f82 100644 --- a/lib/DBIx/Class/ResultSetInstance.pm +++ b/lib/DBIx/Class/ResultSetInstance.pm @@ -8,6 +8,7 @@ sub search_like { shift->resultset_instance->search_like(@_); } sub count { shift->resultset_instance->count(@_); } sub count_literal { shift->resultset_instance->count_literal(@_); } sub find { shift->resultset_instance->find(@_); } -sub create { shift->resultset_instance->create(@_); } +sub create { shift->resultset_instance->create(@_); } +sub find_or_create { shift->resultset_instance->find_or_create(@_); } 1; diff --git a/lib/DBIx/Class/TableInstance.pm b/lib/DBIx/Class/TableInstance.pm index 9f1fc30..51966ee 100644 --- a/lib/DBIx/Class/TableInstance.pm +++ b/lib/DBIx/Class/TableInstance.pm @@ -75,22 +75,6 @@ sub table { $class->mk_classdata('table_instance' => $table); } -=head2 find_or_create - - $class->find_or_create({ key => $val, ... }); - -Searches for a record matching the search condition; if it doesn't find one, -creates one and returns that instead. - -=cut - -sub find_or_create { - my $class = shift; - my $hash = ref $_[0] eq "HASH" ? shift: {@_}; - my $exists = $class->find($hash); - return defined($exists) ? $exists : $class->create($hash); -} - =head2 has_column if ($obj->has_column($col)) { ... }