X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBelongsTo.pm;h=0a0f0dbd2206e297f0364eff6674646ded156f66;hb=dc7d89911b7bb98c30208cf73af522a99998dcd6;hp=fbc78c18c8f438ddae3077a12b1caf2074359c67;hpb=56ad42bb48befbeb50953d197e7cb86bbc62686c;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index fbc78c1..0a0f0db 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -6,7 +6,7 @@ package # hide from PAUSE use strict; use warnings; -use Try::Tiny; +use DBIx::Class::_Util qw( dbic_internal_try dbic_internal_catch ); use namespace::clean; our %_pod_inherit_config = @@ -25,31 +25,43 @@ sub belongs_to { # no join condition or just a column name if (!ref $cond) { - $class->ensure_class_loaded($f_class); - my %f_primaries = map { $_ => 1 } try { $f_class->result_source_instance->_pri_cols_or_die } - catch { - $class->throw_exception( "Can't infer join condition for '$rel' on ${class}: $_"); - }; - my ($pri, $too_many) = keys %f_primaries; - $class->throw_exception( - "Can't infer join condition for '$rel' on ${class}: " - . "${f_class} has multiple primary keys" - ) if $too_many; + my ($f_key, $guess); + if (defined $cond and length $cond) { + $f_key = $cond; + $guess = "caller specified foreign key '$f_key'"; + } + else { + $f_key = $rel; + $guess = "using given relationship name '$rel' as foreign key column name"; + } - my $fk = defined $cond ? $cond : $rel; $class->throw_exception( - "Can't infer join condition for '$rel' on ${class}: " - . "'$fk' is not a column of $class" - ) unless $class->has_column($fk); + "No such column '$f_key' declared yet on ${class} ($guess)" + ) unless $class->result_source->has_column($f_key); + + $class->ensure_class_loaded($f_class); + my $f_rsrc = dbic_internal_try { + $f_class->result_source; + } + dbic_internal_catch { + $class->throw_exception( + "Foreign class '$f_class' does not seem to be a Result class " + . "(or it simply did not load entirely due to a circular relation chain): $_" + ); + }; - $cond = { "foreign.${pri}" => "self.${fk}" }; + my $pri = $f_rsrc->_single_pri_col_or_die; + + $cond = { "foreign.${pri}" => "self.${f_key}" }; } # explicit join condition - elsif (ref $cond) { + else { if (ref $cond eq 'HASH') { # ARRAY is also valid my $cond_rel; + # FIXME This loop is ridiculously incomplete and dangerous + # staving off changes until implmentation of the swindon consensus for (keys %$cond) { if (m/\./) { # Explicit join condition $cond_rel = $cond; @@ -60,13 +72,6 @@ sub belongs_to { $cond = $cond_rel; } } - # 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' @@ -75,7 +80,7 @@ sub belongs_to { and (keys %$cond)[0] =~ /^foreign\./ and - $class->has_column($rel) + $class->result_source->has_column($rel) ) ? 'filter' : 'single'; my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH') @@ -86,6 +91,7 @@ sub belongs_to { $class->add_relationship($rel, $f_class, $cond, { + is_depends_on => 1, accessor => $acc_type, $fk_columns ? ( fk_columns => $fk_columns ) : (), %{$attrs || {}} @@ -95,14 +101,4 @@ sub belongs_to { return 1; } -# Attempt to remove the POD so it (maybe) falls off the indexer - -#=head1 AUTHORS -# -#Alexander Hartmaier -# -#Matt S. Trout -# -#=cut - 1;