1 package # hide from PAUSE
2 DBIx::Class::Relationship::BelongsTo;
4 # Documentation for these methods can be found in
5 # DBIx::Class::Relationship
11 our %_pod_inherit_config =
13 class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
17 my ($class, $rel, $f_class, $cond, $attrs) = @_;
19 # assume a foreign key contraint unless defined otherwise
20 $attrs->{is_foreign_key_constraint} = 1
21 if not exists $attrs->{is_foreign_key_constraint};
22 $attrs->{undef_on_null_fk} = 1
23 if not exists $attrs->{undef_on_null_fk};
25 # no join condition or just a column name
27 $class->ensure_class_loaded($f_class);
28 my %f_primaries = map { $_ => 1 } try { $f_class->_pri_cols }
30 $class->throw_exception( "Can't infer join condition for ${rel} on ${class}: $_");
33 my ($pri, $too_many) = keys %f_primaries;
34 $class->throw_exception(
35 "Can't infer join condition for ${rel} on ${class}; ".
36 "${f_class} has multiple primary keys"
39 my $fk = defined $cond ? $cond : $rel;
40 $class->throw_exception(
41 "Can't infer join condition for ${rel} on ${class}; ".
42 "$fk is not a column of $class"
43 ) unless $class->has_column($fk);
45 my $acc_type = $class->has_column($rel) ? 'filter' : 'single';
46 $class->add_relationship($rel, $f_class,
47 { "foreign.${pri}" => "self.${fk}" },
48 { accessor => $acc_type, %{$attrs || {}} }
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->{$_};
64 my $acc_type = ((ref $cond eq 'HASH')
66 && $class->has_column($rel))
69 $class->add_relationship($rel, $f_class,
71 { accessor => $acc_type, %{$attrs || {}} }
75 $class->throw_exception(
76 'third argument for belongs_to must be undef, a column name, '.
83 # Attempt to remove the POD so it (maybe) falls off the indexer
87 #Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
89 #Matt S. Trout <mst@shadowcatsystems.co.uk>