1 package DBIx::Class::Relationship::BelongsTo;
7 my ($class, $rel, $f_class, $cond, $attrs) = @_;
8 eval "require $f_class";
10 $class->throw_exception($@) unless $@ =~ /Can't locate/;
14 $f_primaries{$_} = 1 for eval { $f_class->primary_columns };
17 # single key relationship
19 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}")
22 my ($pri, $too_many) = keys %f_primaries;
23 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys")
25 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary key")
28 my $fk = defined $cond ? $cond : $rel;
29 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; $fk is not a column")
30 unless $class->has_column($fk);
32 my $acc_type = $class->has_column($rel) ? 'filter' : 'single';
33 $class->add_relationship($rel, $f_class,
34 { "foreign.${pri}" => "self.${fk}" },
35 { accessor => $acc_type, %{$attrs || {}} }
38 # multiple key relationship
39 elsif (ref $cond eq 'HASH') {
42 if (m/\./) { # Explicit join condition
46 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
48 my $acc_type = (keys %$cond_rel == 1 and $class->has_column($rel)) ? 'filter' : 'single';
49 $class->add_relationship($rel, $f_class,
51 { accessor => $acc_type, %{$attrs || {}} }
55 $class->throw_exception('third argument for belongs_to must be undef, a column name, or a join condition');
62 Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
64 Matt S. Trout <mst@shadowcatsystems.co.uk>