1 package # hide from PAUSE
2 DBIx::Class::Relationship::BelongsTo;
4 # Documentation for these methods can be found in
5 # DBIx::Class::Relationship
12 our %_pod_inherit_config =
14 class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
17 sub belongs_to { goto \&refers_to }
20 my ($class, $rel, $f_class, $cond, $attrs) = @_;
22 # assume a foreign key contraint unless defined otherwise
23 $attrs->{is_foreign_key_constraint} = 1
24 if not exists $attrs->{is_foreign_key_constraint};
25 $attrs->{undef_on_null_fk} = 1
26 if not exists $attrs->{undef_on_null_fk};
28 # no join condition or just a column name
30 $class->ensure_class_loaded($f_class);
31 my %f_primaries = map { $_ => 1 } try { $f_class->_pri_cols }
33 $class->throw_exception( "Can't infer join condition for '$rel' on ${class}: $_");
36 my ($pri, $too_many) = keys %f_primaries;
37 $class->throw_exception(
38 "Can't infer join condition for '$rel' on ${class}: "
39 . "${f_class} has multiple primary keys"
42 my $fk = defined $cond ? $cond : $rel;
43 $class->throw_exception(
44 "Can't infer join condition for '$rel' on ${class}: "
45 . "'$fk' is not a column of $class"
46 ) unless $class->has_column($fk);
48 $cond = { "foreign.${pri}" => "self.${fk}" };
51 # explicit join condition
53 if (ref $cond eq 'HASH') { # ARRAY is also valid
56 if (m/\./) { # Explicit join condition
60 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
67 $class->throw_exception(
68 'third argument for belongs_to must be undef, a column name, '.
78 $class->has_column($rel)
79 ) ? 'filter' : 'single';
81 my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
82 ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
86 $class->add_relationship($rel, $f_class,
89 accessor => $acc_type,
90 $fk_columns ? ( fk_columns => $fk_columns ) : (),
98 # Attempt to remove the POD so it (maybe) falls off the indexer
102 #Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
104 #Matt S. Trout <mst@shadowcatsystems.co.uk>