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; |
ed7ab0f4 |
9 | use Try::Tiny; |
fd323bf1 |
10 | use namespace::clean; |
07037f89 |
11 | |
fd323bf1 |
12 | our %_pod_inherit_config = |
044e70c7 |
13 | ( |
14 | class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' } |
15 | ); |
16 | |
07037f89 |
17 | sub belongs_to { |
18 | my ($class, $rel, $f_class, $cond, $attrs) = @_; |
6bf6ba2f |
19 | |
4a0eed52 |
20 | # assume a foreign key constraint unless defined otherwise |
fd323bf1 |
21 | $attrs->{is_foreign_key_constraint} = 1 |
6bf6ba2f |
22 | if not exists $attrs->{is_foreign_key_constraint}; |
cef1bdda |
23 | $attrs->{undef_on_null_fk} = 1 |
24 | if not exists $attrs->{undef_on_null_fk}; |
6bf6ba2f |
25 | |
aeb1bf75 |
26 | # no join condition or just a column name |
7b601771 |
27 | if (!ref $cond) { |
fd4df975 |
28 | $class->ensure_class_loaded($f_class); |
1e3bc087 |
29 | |
0b0743af |
30 | my $pri = $f_class->result_source_instance->_single_pri_col_or_die; |
31 | |
32 | my ($f_key, $guess); |
33 | if (defined $cond and length $cond) { |
34 | $f_key = $cond; |
35 | $guess = "caller specified foreign key '$f_key'"; |
36 | } |
37 | else { |
38 | $f_key = $rel; |
39 | $guess = "using given relationship name '$rel' as foreign key column name"; |
40 | } |
1e3bc087 |
41 | |
fd4df975 |
42 | $class->throw_exception( |
0b0743af |
43 | "No such column '$f_key' declared yet on ${class} ($guess)" |
44 | ) unless $class->has_column($f_key); |
1e3bc087 |
45 | |
0b0743af |
46 | $cond = { "foreign.${pri}" => "self.${f_key}" }; |
35f5c265 |
47 | |
07037f89 |
48 | } |
aeb1bf75 |
49 | # explicit join condition |
1adea0d3 |
50 | else { |
4ff1b819 |
51 | if (ref $cond eq 'HASH') { # ARRAY is also valid |
52 | my $cond_rel; |
53 | for (keys %$cond) { |
54 | if (m/\./) { # Explicit join condition |
55 | $cond_rel = $cond; |
56 | last; |
57 | } |
58 | $cond_rel->{"foreign.$_"} = "self.".$cond->{$_}; |
99be059e |
59 | } |
4ff1b819 |
60 | $cond = $cond_rel; |
07037f89 |
61 | } |
07037f89 |
62 | } |
35f5c265 |
63 | |
64 | my $acc_type = ( |
65 | ref $cond eq 'HASH' |
66 | and |
67 | keys %$cond == 1 |
68 | and |
6dd43920 |
69 | (keys %$cond)[0] =~ /^foreign\./ |
70 | and |
35f5c265 |
71 | $class->has_column($rel) |
72 | ) ? 'filter' : 'single'; |
73 | |
74 | my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH') |
75 | ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) } |
76 | : undef |
77 | ; |
78 | |
79 | $class->add_relationship($rel, $f_class, |
80 | $cond, |
81 | { |
82 | accessor => $acc_type, |
83 | $fk_columns ? ( fk_columns => $fk_columns ) : (), |
84 | %{$attrs || {}} |
85 | } |
86 | ); |
87 | |
07037f89 |
88 | return 1; |
89 | } |
90 | |
e09b8a14 |
91 | # Attempt to remove the POD so it (maybe) falls off the indexer |
503536d5 |
92 | |
e09b8a14 |
93 | #=head1 AUTHORS |
94 | # |
95 | #Alexander Hartmaier <Alexander.Hartmaier@t-systems.at> |
96 | # |
97 | #Matt S. Trout <mst@shadowcatsystems.co.uk> |
98 | # |
99 | #=cut |
503536d5 |
100 | |
07037f89 |
101 | 1; |