X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBelongsTo.pm;h=76ffb50cda5b0ab3f6ce76d86f8e848be6fef4a4;hb=524fc24c685553411256485016ce2466cf914ab0;hp=0f19bd2a35244a6e59049bc42b9b76330eeccc9d;hpb=fd323bf1046faa7de5a8c985268d80ec5b703361;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index 0f19bd2..76ffb50 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -43,11 +43,8 @@ sub belongs_to { "$fk is not a column of $class" ) unless $class->has_column($fk); - my $acc_type = $class->has_column($rel) ? 'filter' : 'single'; - $class->add_relationship($rel, $f_class, - { "foreign.${pri}" => "self.${fk}" }, - { accessor => $acc_type, %{$attrs || {}} } - ); + $cond = { "foreign.${pri}" => "self.${fk}" }; + } # explicit join condition elsif (ref $cond) { @@ -62,22 +59,37 @@ sub belongs_to { } $cond = $cond_rel; } - my $acc_type = ((ref $cond eq 'HASH') - && keys %$cond == 1 - && $class->has_column($rel)) - ? 'filter' - : 'single'; - $class->add_relationship($rel, $f_class, - $cond, - { accessor => $acc_type, %{$attrs || {}} } - ); } + # dunno else { $class->throw_exception( 'third argument for belongs_to must be undef, a column name, '. 'or a join condition' ); } + + my $acc_type = ( + ref $cond eq 'HASH' + and + keys %$cond == 1 + and + $class->has_column($rel) + ) ? 'filter' : 'single'; + + my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH') + ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) } + : undef + ; + + $class->add_relationship($rel, $f_class, + $cond, + { + accessor => $acc_type, + $fk_columns ? ( fk_columns => $fk_columns ) : (), + %{$attrs || {}} + } + ); + return 1; }