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