From: Matt S Trout Date: Tue, 30 Sep 2008 23:25:33 +0000 (+0000) Subject: fix new_related on uninserted objects to handle has_manys correctly X-Git-Tag: v0.08240~340 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2c5c07ecc0a7e4e54854aefdd5f3e44cb71cf5e7;p=dbsrgits%2FDBIx-Class.git fix new_related on uninserted objects to handle has_manys correctly --- diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index dd84ef4..b64e455 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -193,8 +193,13 @@ sub related_resultset { if ($cond eq $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION) { my $reverse = $source->reverse_relationship_info($rel); foreach my $rev_rel (keys %$reverse) { - $attrs->{related_objects}{$rev_rel} = $self; - Scalar::Util::weaken($attrs->{related_object}{$rev_rel}); + if ($reverse->{$rev_rel}{attrs}{accessor} eq 'multi') { + $attrs->{related_objects}{$rev_rel} = [ $self ]; + Scalar::Util::weaken($attrs->{related_object}{$rev_rel}[0]); + } else { + $attrs->{related_objects}{$rev_rel} = $self; + Scalar::Util::weaken($attrs->{related_object}{$rev_rel}); + } } } if (ref $cond eq 'ARRAY') { diff --git a/t/66relationship.t b/t/66relationship.t index e262230..ddde6b2 100644 --- a/t/66relationship.t +++ b/t/66relationship.t @@ -7,7 +7,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 64; +plan tests => 65; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -213,7 +213,11 @@ is( $twokey->fourkeys_to_twokeys->count, 0, my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 }); is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded'); is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db'); - +eval{ + $undef_artist_cd->related_resultset('artist')->new({name => 'foo'}); +}; +is( $@, '', "Object created on a resultset related to not yet inserted object"); + my $def_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007, artist => undef }); is($def_artist_cd->has_column_loaded('artist'), 1, 'FK loaded'); is($def_artist_cd->search_related('artist')->count, 0, 'closed search on null FK');