1 package # hide from PAUSE
2 DBIx::Class::Relationship::BelongsTo;
4 # Documentation for these methods can be found in
5 # DBIx::Class::Relationship
10 use DBIx::Class::_Util 'dbic_internal_try';
13 our %_pod_inherit_config =
15 class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
19 my ($class, $rel, $f_class, $cond, $attrs) = @_;
21 # assume a foreign key constraint unless defined otherwise
22 $attrs->{is_foreign_key_constraint} = 1
23 if not exists $attrs->{is_foreign_key_constraint};
24 $attrs->{undef_on_null_fk} = 1
25 if not exists $attrs->{undef_on_null_fk};
27 # no join condition or just a column name
31 if (defined $cond and length $cond) {
33 $guess = "caller specified foreign key '$f_key'";
37 $guess = "using given relationship name '$rel' as foreign key column name";
40 $class->throw_exception(
41 "No such column '$f_key' declared yet on ${class} ($guess)"
42 ) unless $class->has_column($f_key);
44 $class->ensure_class_loaded($f_class);
45 my $f_rsrc = dbic_internal_try {
46 $f_class->result_source_instance;
49 $class->throw_exception(
50 "Foreign class '$f_class' does not seem to be a Result class "
51 . "(or it simply did not load entirely due to a circular relation chain)"
55 my $pri = $f_rsrc->_single_pri_col_or_die;
57 $cond = { "foreign.${pri}" => "self.${f_key}" };
60 # explicit join condition
62 if (ref $cond eq 'HASH') { # ARRAY is also valid
64 # FIXME This loop is ridiculously incomplete and dangerous
65 # staving off changes until implmentation of the swindon consensus
67 if (m/\./) { # Explicit join condition
71 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
82 (keys %$cond)[0] =~ /^foreign\./
84 $class->has_column($rel)
85 ) ? 'filter' : 'single';
87 my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
88 ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
92 $class->add_relationship($rel, $f_class,
96 accessor => $acc_type,
97 $fk_columns ? ( fk_columns => $fk_columns ) : (),