From: Matt S Trout Date: Wed, 19 Apr 2006 22:01:31 +0000 (+0000) Subject: make set_from_related handle undef X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2c037e6bd80a384bbf00b46ad91a7b36251f080b;p=dbsrgits%2FDBIx-Class-Historic.git make set_from_related handle undef --- diff --git a/Changes b/Changes index e9cdf80..f32294b 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for DBIx::Class 0.06002 + - fix set_from_related to accept undef - fix to Dumper-induced hash iteration bug - fix to copy() with non-composed resultsource - fix to ->search without args to clone rs but maintain cache diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 6e6a4f4..96b7c18 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -276,9 +276,11 @@ sub set_from_related { "condition for $rel is of type ". (ref $cond ? ref $cond : 'plain scalar') ) unless ref $cond eq 'HASH'; - my $f_class = $self->result_source->schema->class($rel_obj->{class}); - $self->throw_exception( "Object $f_obj isn't a ".$f_class ) - unless $f_obj->isa($f_class); + if (defined $f_obj) { + my $f_class = $self->result_source->schema->class($rel_obj->{class}); + $self->throw_exception( "Object $f_obj isn't a ".$f_class ) + unless $f_obj->isa($f_class); + } $self->set_columns( $self->result_source->resolve_condition( $rel_obj->{cond}, $f_obj, $rel)); diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index e54e3fc..8f059ed 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -519,8 +519,12 @@ sub resolve_condition { #warn "$self $k $for $v"; $ret{$k} = $for->get_column($v); #warn %ret; + } elsif (!defined $for) { # undef, i.e. "no object" + $ret{$k} = undef; } elsif (ref $as) { # reverse object $ret{$v} = $as->get_column($k); + } elsif (!defined $as) { # undef, i.e. "no reverse object" + $ret{$v} = undef; } else { $ret{"${as}.${k}"} = "${for}.${v}"; } diff --git a/t/run/06relationship.tl b/t/run/06relationship.tl index 04d1e36..bc84c2e 100644 --- a/t/run/06relationship.tl +++ b/t/run/06relationship.tl @@ -3,7 +3,7 @@ my $schema = shift; use strict; use warnings; -plan tests => 25; +plan tests => 26; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -50,12 +50,17 @@ my $track = $schema->resultset("Track")->create( { } ); $track->set_from_related( cd => $cd ); -if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object +if ($INC{'DBICTest/HelperRels.pm'}) { # expect inflated object is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' ); } else { is( $track->cd, 4, 'set_from_related ok' ); } +$track->set_from_related( cd => undef ); + +ok( !defined($track->cd), 'set_from_related with undef ok'); + + # update_from_related, the same as set_from_related, but it calls update afterwards $track = $schema->resultset("Track")->create( { trackid => 2,