From: Johannes Plunien Date: Fri, 17 Apr 2009 08:10:19 +0000 (+0000) Subject: Added update_or_new to ResultSet.pm X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9ffa8fd71f7e3e8c81b785f2e49353af93c13aa9;p=dbsrgits%2FDBIx-Class-Historic.git Added update_or_new to ResultSet.pm --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 816b374..38f4169 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2112,6 +2112,63 @@ sub update_or_create { return $self->create($cond); } +=head2 update_or_new + +=over 4 + +=item Arguments: \%col_values, { key => $unique_constraint }? + +=item Return Value: $rowobject + +=back + + $resultset->update_or_new({ col => $val, ... }); + +First, searches for an existing row matching one of the unique constraints +(including the primary key) on the source of this resultset. If a row is +found, updates it with the other given column values. Otherwise, instantiate +a new result object and return it. The object will not be saved into your storage +until you call L on it. + +Takes an optional C attribute to search on a specific unique constraint. +For example: + + # In your application + my $cd = $schema->resultset('CD')->update_or_new( + { + artist => 'Massive Attack', + title => 'Mezzanine', + year => 1998, + }, + { key => 'cd_artist_title' } + ); + + if ($cd->in_storage) { + # the cd was updated + } + else { + # the cd is not yet in the database, let's insert it + $cd->insert; + } + +See also L, L and L. + +=cut + +sub update_or_new { + my $self = shift; + my $attrs = ( @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {} ); + my $cond = ref $_[0] eq 'HASH' ? shift : {@_}; + + my $row = $self->find( $cond, $attrs ); + if ( defined $row ) { + $row->update($cond); + return $row; + } + + return $self->new_result($cond); +} + =head2 get_cache =over 4 diff --git a/t/80unique.t b/t/80unique.t index 6108f28..8bd7e2c 100644 --- a/t/80unique.t +++ b/t/80unique.t @@ -7,7 +7,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 45; +plan tests => 49; # Check the defined unique constraints is_deeply( @@ -183,3 +183,30 @@ is($row->baz, 3, 'baz is correct'); ok($cd->in_storage, 'find correctly grepped the key across a relationship'); is($cd->cdid, 1, 'cdid is correct'); } + +# Test update_or_new +{ + my $cd1 = $schema->resultset('CD')->update_or_new( + { + artist => $artistid, + title => "SuperHits $$", + year => 2007, + }, + { key => 'cd_artist_title' } + ); + + ok(!$cd1->in_storage, 'CD is not in storage yet after update_or_new'); + $cd1->insert; + ok($cd1->in_storage, 'CD got added to strage after update_or_new && insert'); + + my $cd2 = $schema->resultset('CD')->update_or_new( + { + artist => $artistid, + title => "SuperHits $$", + year => 2008, + }, + { key => 'cd_artist_title' } + ); + ok($cd2->in_storage, 'Updating year using update_or_new was successful'); + is($cd2->id, $cd1->id, 'Got the same CD using update_or_new'); +} \ No newline at end of file