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/;
13 # no join condition or just a column name
15 my %f_primaries = map { $_ => 1 } eval { $f_class->primary_columns };
16 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}")
19 my ($pri, $too_many) = keys %f_primaries;
20 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys")
22 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary keys")
25 my $fk = defined $cond ? $cond : $rel;
26 $class->throw_exception("Can't infer join condition for ${rel} on ${class}; $fk is not a column")
27 unless $class->has_column($fk);
29 my $acc_type = $class->has_column($rel) ? 'filter' : 'single';
30 $class->add_relationship($rel, $f_class,
31 { "foreign.${pri}" => "self.${fk}" },
32 { accessor => $acc_type, %{$attrs || {}} }
35 # explicit join condition
36 elsif (ref $cond eq 'HASH') {
39 if (m/\./) { # Explicit join condition
43 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
45 my $acc_type = (keys %$cond_rel == 1 and $class->has_column($rel)) ? 'filter' : 'single';
46 $class->add_relationship($rel, $f_class,
48 { accessor => $acc_type, %{$attrs || {}} }
52 $class->throw_exception('third argument for belongs_to must be undef, a column name, or a join condition');
59 Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
61 Matt S. Trout <mst@shadowcatsystems.co.uk>