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<DBIx::Class::Row/insert> on it.
+
+Takes an optional C<key> 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</find>, L</find_or_create> and L<find_or_new>.
+
+=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
my $schema = DBICTest->init_schema();
-plan tests => 45;
+plan tests => 49;
# Check the defined unique constraints
is_deeply(
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