From: David Kamholz Date: Sun, 11 Dec 2005 09:18:54 +0000 (+0000) Subject: rewrite belongs_to again, so $cond is foreign key in current table X-Git-Tag: v0.05005~141 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1e3bc087549802d6761d11184eee1ee32b9dc797;p=dbsrgits%2FDBIx-Class.git rewrite belongs_to again, so $cond is foreign key in current table --- diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index ef6f904..b1ef52a 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -16,19 +16,22 @@ sub belongs_to { # single key relationship if (!ref $cond) { - my ($pri,$too_many); - if (!defined $cond) { - $class->throw("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}") unless $f_loaded; - ($pri, $too_many) = keys %f_primaries; - $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys") unless defined $pri; - $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary key") if $too_many; - } - else { - $pri = $cond; - } - my $acc_type = ($class->has_column($rel)) ? 'filter' : 'single'; + $class->throw("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}") + unless $f_loaded; + + my ($pri, $too_many) = keys %f_primaries; + $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys") + unless defined $pri; + $class->throw("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary key") + if $too_many; + + my $fk = defined $cond ? $cond : $rel; + $class->throw("Can't infer join condition for ${rel} on ${class}; $fk is not a column") + unless $class->has_column($fk); + + my $acc_type = $class->has_column($rel) ? 'filter' : 'single'; $class->add_relationship($rel, $f_class, - { "foreign.${pri}" => "self.${rel}" }, + { "foreign.${pri}" => "self.${fk}" }, { accessor => $acc_type, %{$attrs || {}} } ); } diff --git a/t/lib/DBICTest/Schema/HelperRels.pm b/t/lib/DBICTest/Schema/HelperRels.pm index 2f99d7b..63191ea 100644 --- a/t/lib/DBICTest/Schema/HelperRels.pm +++ b/t/lib/DBICTest/Schema/HelperRels.pm @@ -26,7 +26,9 @@ DBICTest::Schema::SelfRef->has_many( DBICTest::Schema::Tag->belongs_to('cd', 'DBICTest::Schema::CD'); -DBICTest::Schema::Track->belongs_to('cd', 'DBICTest::Schema::CD', 'cdid'); +DBICTest::Schema::Track->belongs_to('cd', 'DBICTest::Schema::CD'); + +DBICTest::Schema::Track->belongs_to('disc', 'DBICTest::Schema::CD', 'cd'); DBICTest::Schema::TwoKeys->belongs_to('artist', 'DBICTest::Schema::Artist'); diff --git a/t/run/06relationship.tl b/t/run/06relationship.tl index 04d2249..5a5c6e5 100644 --- a/t/run/06relationship.tl +++ b/t/run/06relationship.tl @@ -48,7 +48,7 @@ my $track = DBICTest::Track->create( { $track->set_from_related( cd => $cd ); if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object - is($track->cd->cdid, 4, 'set_from_related ok' ); + is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' ); } else { is( $track->cd, 4, 'set_from_related ok' ); }