X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBelongsTo.pm;h=50ddc2eb446ada4e0dff0f2ae27817eaabce50c6;hb=e570488ade8f327f47dd3318db3443a348d561d6;hp=cbc44849710d47ab9762b3377feabb1d6b3376a8;hpb=0b0743afcb6904727aa51cc39fabeea190f5dac6;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index cbc4484..50ddc2e 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -7,6 +7,7 @@ package # hide from PAUSE use strict; use warnings; use Try::Tiny; +use DBIx::Class::_Util 'dbic_internal_try'; use namespace::clean; our %_pod_inherit_config = @@ -25,9 +26,6 @@ sub belongs_to { # no join condition or just a column name if (!ref $cond) { - $class->ensure_class_loaded($f_class); - - my $pri = $f_class->result_source_instance->_single_pri_col_or_die; my ($f_key, $guess); if (defined $cond and length $cond) { @@ -41,7 +39,20 @@ sub belongs_to { $class->throw_exception( "No such column '$f_key' declared yet on ${class} ($guess)" - ) unless $class->has_column($f_key); + ) unless $class->result_source->has_column($f_key); + + $class->ensure_class_loaded($f_class); + my $f_rsrc = dbic_internal_try { + $f_class->result_source; + } + 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): $_" + ); + }; + + my $pri = $f_rsrc->_single_pri_col_or_die; $cond = { "foreign.${pri}" => "self.${f_key}" }; @@ -50,6 +61,8 @@ sub belongs_to { 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; @@ -68,7 +81,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') @@ -79,6 +92,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 || {}} @@ -88,14 +102,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;