X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBelongsTo.pm;h=050c1e4af0f409dfef93d2e6e08c362e5b7cca7b;hb=ed7ab0f4ce1a9118ea6285ee562ef003085a6b64;hp=4201835a5747ff5b50aa5d1d9271e3534288be73;hpb=99be059e6d635374f075f430af6e7cd6ee18ab67;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index 4201835..050c1e4 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -1,53 +1,93 @@ -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; + +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"; - # single key relationship - if (not defined $cond) { - my ($pri, $too_many) = keys %{ $f_class->_primaries }; - my $acc_type = ($class->_columns->{$rel}) ? 'filter' : 'single'; + + # assume a foreign key contraint 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) { + $class->ensure_class_loaded($f_class); + my %f_primaries = map { $_ => 1 } try { $f_class->_pri_cols } + 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 $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); + + my $acc_type = $class->has_column($rel) ? 'filter' : 'single'; $class->add_relationship($rel, $f_class, - { "foreign.${pri}" => "self.${rel}" }, - { accessor => $acc_type } + { "foreign.${pri}" => "self.${fk}" }, + { accessor => $acc_type, %{$attrs || {}} } ); } - # multiple key relationship - else { - my %f_primaries = eval { %{ $f_class->_primaries } }; - my $f_loaded = !$@; - my $cond_rel; - for (keys %$cond) { - if (m/\./) { # Explicit join condition - $cond_rel = $cond; - last; - } - $cond_rel->{"foreign.$_"} = "self.".$cond->{$_}; - # primary key usage checks - if (exists $f_primaries{$_}) { - delete $f_primaries{$_}; - } elsif ($f_loaded) { - $class->throw("non primary key used in join condition: $_"); + # explicit join condition + elsif (ref $cond) { + if (ref $cond eq 'HASH') { # ARRAY is also valid + my $cond_rel; + for (keys %$cond) { + if (m/\./) { # Explicit join condition + $cond_rel = $cond; + last; + } + $cond_rel->{"foreign.$_"} = "self.".$cond->{$_}; } + $cond = $cond_rel; } - $class->throw("not all primary keys used in multi key relationship!") if $f_loaded && keys %f_primaries; + my $acc_type = ((ref $cond eq 'HASH') + && keys %$cond == 1 + && $class->has_column($rel)) + ? 'filter' + : 'single'; $class->add_relationship($rel, $f_class, - $cond_rel, - { accessor => 'single', %{$attrs ||{}} } + $cond, + { accessor => $acc_type, %{$attrs || {}} } + ); + } + else { + $class->throw_exception( + 'third argument for belongs_to must be undef, a column name, '. + 'or a join condition' ); } return 1; } -=head1 AUTHORS - -Alexander Hartmaier - -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;