1 package # hide from PAUSE
2 DBIx::Class::Relationship::BelongsTo;
4 # Documentation for these methods can be found in
5 # DBIx::Class::Relationship
10 our %_pod_inherit_config =
12 class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
16 my ($class, $rel, $f_class, $cond, $attrs) = @_;
18 # assume a foreign key contraint unless defined otherwise
19 $attrs->{is_foreign_key_constraint} = 1
20 if not exists $attrs->{is_foreign_key_constraint};
21 $attrs->{undef_on_null_fk} = 1
22 if not exists $attrs->{undef_on_null_fk};
24 # no join condition or just a column name
26 $class->ensure_class_loaded($f_class);
27 my %f_primaries = map { $_ => 1 } eval { $f_class->primary_columns };
28 $class->throw_exception(
29 "Can't infer join condition for ${rel} on ${class}; ".
30 "unable to load ${f_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 no primary keys"
37 ) unless defined $pri;
38 $class->throw_exception(
39 "Can't infer join condition for ${rel} on ${class}; ".
40 "${f_class} has multiple primary keys"
43 my $fk = defined $cond ? $cond : $rel;
44 $class->throw_exception(
45 "Can't infer join condition for ${rel} on ${class}; ".
46 "$fk is not a column of $class"
47 ) unless $class->has_column($fk);
49 my $acc_type = $class->has_column($rel) ? 'filter' : 'single';
50 $class->add_relationship($rel, $f_class,
51 { "foreign.${pri}" => "self.${fk}" },
52 { accessor => $acc_type, %{$attrs || {}} }
55 # explicit join condition
57 if (ref $cond eq 'HASH') { # ARRAY is also valid
60 if (m/\./) { # Explicit join condition
64 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
68 my $acc_type = ((ref $cond eq 'HASH')
70 && $class->has_column($rel))
73 $class->add_relationship($rel, $f_class,
75 { accessor => $acc_type, %{$attrs || {}} }
79 $class->throw_exception(
80 'third argument for belongs_to must be undef, a column name, '.
87 # Attempt to remove the POD so it (maybe) falls off the indexer
91 #Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
93 #Matt S. Trout <mst@shadowcatsystems.co.uk>