1 package DBIx::Class::Relationship::BelongsTo;
7 my ($class, $rel, $f_class, $cond, $attrs) = @_;
8 $class->ensure_class_loaded($f_class);
9 # no join condition or just a column name
11 my %f_primaries = map { $_ => 1 } eval { $f_class->primary_columns };
12 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}")
15 my ($pri, $too_many) = keys %f_primaries;
16 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys")
18 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary keys")
21 my $fk = defined $cond ? $cond : $rel;
22 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; $fk is not a column")
23 unless $class->has_column($fk);
25 my $acc_type = $class->has_column($rel) ? 'filter' : 'single';
26 $class->add_relationship($rel, $f_class,
27 { "foreign.${pri}" => "self.${fk}" },
28 { accessor => $acc_type, %{$attrs || {}} }
31 # explicit join condition
32 elsif (ref $cond eq 'HASH') {
35 if (m/\./) { # Explicit join condition
39 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
41 my $acc_type = (keys %$cond_rel == 1 and $class->has_column($rel)) ? 'filter' : 'single';
42 $class->add_relationship($rel, $f_class,
44 { accessor => $acc_type, %{$attrs || {}} }
48 $class->throw_exception('third argument for belongs_to must be undef, a column name, or a join condition');
55 Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
57 Matt S. Trout <mst@shadowcatsystems.co.uk>