Entirely and utterly rewrite populate(), fixing the variable hash issue
[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
07037f89 17sub 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) {
0b0743af 28
29 my ($f_key, $guess);
30 if (defined $cond and length $cond) {
31 $f_key = $cond;
32 $guess = "caller specified foreign key '$f_key'";
33 }
34 else {
35 $f_key = $rel;
36 $guess = "using given relationship name '$rel' as foreign key column name";
37 }
1e3bc087 38
fd4df975 39 $class->throw_exception(
0b0743af 40 "No such column '$f_key' declared yet on ${class} ($guess)"
41 ) unless $class->has_column($f_key);
1e3bc087 42
9e7525a2 43 $class->ensure_class_loaded($f_class);
44 my $f_rsrc = try {
45 $f_class->result_source_instance;
46 }
47 catch {
48 $class->throw_exception(
49 "Foreign class '$f_class' does not seem to be a Result class "
50 . "(or it simply did not load entirely due to a circular relation chain)"
51 );
52 };
53
54 my $pri = $f_rsrc->_single_pri_col_or_die;
55
0b0743af 56 $cond = { "foreign.${pri}" => "self.${f_key}" };
35f5c265 57
07037f89 58 }
aeb1bf75 59 # explicit join condition
1adea0d3 60 else {
4ff1b819 61 if (ref $cond eq 'HASH') { # ARRAY is also valid
62 my $cond_rel;
63 for (keys %$cond) {
64 if (m/\./) { # Explicit join condition
65 $cond_rel = $cond;
66 last;
67 }
68 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
99be059e 69 }
4ff1b819 70 $cond = $cond_rel;
07037f89 71 }
07037f89 72 }
35f5c265 73
74 my $acc_type = (
75 ref $cond eq 'HASH'
76 and
77 keys %$cond == 1
78 and
6dd43920 79 (keys %$cond)[0] =~ /^foreign\./
80 and
35f5c265 81 $class->has_column($rel)
82 ) ? 'filter' : 'single';
83
84 my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
85 ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
86 : undef
87 ;
88
89 $class->add_relationship($rel, $f_class,
90 $cond,
91 {
d0cefd99 92 is_depends_on => 1,
35f5c265 93 accessor => $acc_type,
94 $fk_columns ? ( fk_columns => $fk_columns ) : (),
95 %{$attrs || {}}
96 }
97 );
98
07037f89 99 return 1;
100}
101
e09b8a14 102# Attempt to remove the POD so it (maybe) falls off the indexer
503536d5 103
e09b8a14 104#=head1 AUTHORS
105#
106#Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
107#
108#Matt S. Trout <mst@shadowcatsystems.co.uk>
109#
110#=cut
503536d5 111
07037f89 1121;