From: Daniel Westermann-Clark Date: Sun, 16 Apr 2006 22:52:34 +0000 (-0400) Subject: Add update_or_create_related X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=045120e6a16429e7475e129bff3cd0d567eb4a89;p=dbsrgits%2FDBIx-Class-Historic.git Add update_or_create_related --- diff --git a/Changes b/Changes index ea392d3..c77d47b 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ Revision history for DBIx::Class - added add_column alias to ResultSourceProxy - added source_name to ResultSource - load_classes now uses source_name and sets it if necessary + - add update_or_create_related 0.06002 - fix for -and conditions when updating or deleting on a ResultSet diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 6e6a4f4..034e5cc 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -252,6 +252,21 @@ sub find_or_create_related { return $self->find_related(@_) || $self->create_related(@_); } +=head2 update_or_create_related + + my $updated_item = $obj->update_or_create_related('relname', \%col_data, \%attrs?); + +Update or create an item of a related class. See +L for details. + +=cut + +sub update_or_create_related { + my $self = shift; + my $rel = shift; + return $self->related_resultset($rel)->update_or_create(@_); +} + =head2 set_from_related $book->set_from_related('author', $author_obj); diff --git a/t/run/20unique.tl b/t/run/20unique.tl index 024f09f..8b6ce3e 100644 --- a/t/run/20unique.tl +++ b/t/run/20unique.tl @@ -1,7 +1,7 @@ sub run_tests { my $schema = shift; -plan tests => 26; +plan tests => 30; my $artistid = 1; my $title = 'UNIQUE Constraint'; @@ -98,6 +98,20 @@ is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); is($cd7->title, $cd1->title, 'title is correct'); is($cd7->year, $cd1->year, 'year is correct'); +my $cd8 = $artist->update_or_create_related('cds', + { + artist => $artistid, + title => $title, + year => 2021, + }, + { key => 'artist_title' } +); + +is($cd8->cdid, $cd1->cdid, 'update or create related by specific key: cdid is correct'); +is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); +is($cd8->title, $cd1->title, 'title is correct'); +is($cd8->year, 2021, 'year is correct'); + } 1;