rename occurences of belongs_to in code and comments
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / BelongsTo.pm
CommitLineData
9b83fccd 1package # hide from PAUSE
2 DBIx::Class::Relationship::BelongsTo;
3
4# Documentation for these methods can be found in
5# DBIx::Class::Relationship
07037f89 6
7use strict;
8use warnings;
ed7ab0f4 9use Try::Tiny;
fd323bf1 10use namespace::clean;
07037f89 11
fd323bf1 12our %_pod_inherit_config =
044e70c7 13 (
14 class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
15 );
16
3420931b 17sub belongs_to { goto \&refers_to }
18
19sub refers_to {
07037f89 20 my ($class, $rel, $f_class, $cond, $attrs) = @_;
6bf6ba2f 21
22 # assume a foreign key contraint unless defined otherwise
fd323bf1 23 $attrs->{is_foreign_key_constraint} = 1
6bf6ba2f 24 if not exists $attrs->{is_foreign_key_constraint};
cef1bdda 25 $attrs->{undef_on_null_fk} = 1
26 if not exists $attrs->{undef_on_null_fk};
6bf6ba2f 27
aeb1bf75 28 # no join condition or just a column name
7b601771 29 if (!ref $cond) {
fd4df975 30 $class->ensure_class_loaded($f_class);
ed7ab0f4 31 my %f_primaries = map { $_ => 1 } try { $f_class->_pri_cols }
32 catch {
e705f529 33 $class->throw_exception( "Can't infer join condition for '$rel' on ${class}: $_");
ed7ab0f4 34 };
1e3bc087 35
36 my ($pri, $too_many) = keys %f_primaries;
fd4df975 37 $class->throw_exception(
e705f529 38 "Can't infer join condition for '$rel' on ${class}: "
39 . "${f_class} has multiple primary keys"
fd4df975 40 ) if $too_many;
1e3bc087 41
42 my $fk = defined $cond ? $cond : $rel;
fd4df975 43 $class->throw_exception(
e705f529 44 "Can't infer join condition for '$rel' on ${class}: "
45 . "'$fk' is not a column of $class"
fd4df975 46 ) unless $class->has_column($fk);
1e3bc087 47
35f5c265 48 $cond = { "foreign.${pri}" => "self.${fk}" };
49
07037f89 50 }
aeb1bf75 51 # explicit join condition
4ff1b819 52 elsif (ref $cond) {
53 if (ref $cond eq 'HASH') { # ARRAY is also valid
54 my $cond_rel;
55 for (keys %$cond) {
56 if (m/\./) { # Explicit join condition
57 $cond_rel = $cond;
58 last;
59 }
60 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
99be059e 61 }
4ff1b819 62 $cond = $cond_rel;
07037f89 63 }
07037f89 64 }
35f5c265 65 # dunno
7b601771 66 else {
fd4df975 67 $class->throw_exception(
5c008e0f 68 'third argument for refers_to must be undef, a column name, '.
fd4df975 69 'or a join condition'
70 );
7b601771 71 }
35f5c265 72
73 my $acc_type = (
74 ref $cond eq 'HASH'
75 and
76 keys %$cond == 1
77 and
78 $class->has_column($rel)
79 ) ? 'filter' : 'single';
80
81 my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
82 ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
83 : undef
84 ;
85
86 $class->add_relationship($rel, $f_class,
87 $cond,
88 {
89 accessor => $acc_type,
90 $fk_columns ? ( fk_columns => $fk_columns ) : (),
91 %{$attrs || {}}
92 }
93 );
94
07037f89 95 return 1;
96}
97
e09b8a14 98# Attempt to remove the POD so it (maybe) falls off the indexer
503536d5 99
e09b8a14 100#=head1 AUTHORS
101#
102#Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
103#
104#Matt S. Trout <mst@shadowcatsystems.co.uk>
105#
106#=cut
503536d5 107
07037f89 1081;