X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBelongsTo.pm;h=f6ce3a5dc2b522a109f944c153e77a2ec59b9658;hb=6909ab3ccc795859d538010083cdafe77e225980;hp=ef6f904ea84aa78749b9f3c2f25558380ca28994;hpb=7b6017716effef9d7467a9d8ef68068b3720a2e0;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index ef6f904..f6ce3a5 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -1,64 +1,114 @@ -package DBIx::Class::Relationship::BelongsTo; +package # hide from PAUSE + DBIx::Class::Relationship::BelongsTo; + +# Documentation for these methods can be found in +# DBIx::Class::Relationship use strict; use warnings; +use Try::Tiny; +use namespace::clean; + +our %_pod_inherit_config = + ( + class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' } + ); sub belongs_to { my ($class, $rel, $f_class, $cond, $attrs) = @_; - eval "require $f_class"; - if ($@) { - $class->throw($@) unless $@ =~ /Can't locate/; - } - my %f_primaries; - $f_primaries{$_} = 1 for eval { $f_class->primary_columns }; - my $f_loaded = !$@; - - # single key relationship + # assume a foreign key constraint unless defined otherwise + $attrs->{is_foreign_key_constraint} = 1 + if not exists $attrs->{is_foreign_key_constraint}; + $attrs->{undef_on_null_fk} = 1 + if not exists $attrs->{undef_on_null_fk}; + + # no join condition or just a column name 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; + + my ($f_key, $guess); + if (defined $cond and length $cond) { + $f_key = $cond; + $guess = "caller specified foreign key '$f_key'"; } else { - $pri = $cond; + $f_key = $rel; + $guess = "using given relationship name '$rel' as foreign key column name"; } - my $acc_type = ($class->has_column($rel)) ? 'filter' : 'single'; - $class->add_relationship($rel, $f_class, - { "foreign.${pri}" => "self.${rel}" }, - { accessor => $acc_type, %{$attrs || {}} } - ); - } - # multiple key relationship - elsif (ref $cond eq 'HASH') { - my $cond_rel; - for (keys %$cond) { - if (m/\./) { # Explicit join condition - $cond_rel = $cond; - last; - } - $cond_rel->{"foreign.$_"} = "self.".$cond->{$_}; + + $class->throw_exception( + "No such column '$f_key' declared yet on ${class} ($guess)" + ) unless $class->has_column($f_key); + + $class->ensure_class_loaded($f_class); + my $f_rsrc = try { + $f_class->result_source_instance; } - $class->add_relationship($rel, $f_class, - $cond_rel, - { accessor => 'single', %{$attrs || {}} } - ); + 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}" }; + } + # explicit join condition else { - $class->throw('third argument for belongs_to must be undef, a column name, or a join condition'); + 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; + last; + } + $cond_rel->{"foreign.$_"} = "self.".$cond->{$_}; + } + $cond = $cond_rel; + } } - return 1; -} -=head1 AUTHORS + my $acc_type = ( + ref $cond eq 'HASH' + and + keys %$cond == 1 + and + (keys %$cond)[0] =~ /^foreign\./ + and + $class->has_column($rel) + ) ? 'filter' : 'single'; -Alexander Hartmaier + 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, + { + is_depends_on => 1, + accessor => $acc_type, + $fk_columns ? ( fk_columns => $fk_columns ) : (), + %{$attrs || {}} + } + ); + + return 1; +} -Matt S. Trout +# Attempt to remove the POD so it (maybe) falls off the indexer -=cut +#=head1 AUTHORS +# +#Alexander Hartmaier +# +#Matt S. Trout +# +#=cut 1;