A couple of typos, and general whitespace cleanup (ick)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / HasMany.pm
CommitLineData
75d07914 1package # hide from PAUSE
c0e7b4e5 2 DBIx::Class::Relationship::HasMany;
07037f89 3
4use strict;
5use warnings;
6
044e70c7 7our %_pod_inherit_config =
8 (
9 class_map => { 'DBIx::Class::Relationship::HasMany' => 'DBIx::Class::Relationship' }
10 );
11
07037f89 12sub has_many {
13 my ($class, $rel, $f_class, $cond, $attrs) = @_;
c037c03a 14
dcf8330b 15 unless (ref $cond) {
fd4df975 16 $class->ensure_class_loaded($f_class);
103647d5 17 my ($pri, $too_many) = $class->primary_columns;
fd4df975 18
19 $class->throw_exception(
20 "has_many can only infer join for a single primary key; ".
21 "${class} has more"
22 ) if $too_many;
aeb1bf75 23
e36de82e 24 $class->throw_exception(
25 "has_many needs a primary key to infer a join; ".
26 "${class} has none"
27 ) if !defined $pri && (!defined $cond || !length $cond);
28
aeb1bf75 29 my ($f_key,$guess);
07037f89 30 if (defined $cond && length $cond) {
31 $f_key = $cond;
dcf8330b 32 $guess = "caller specified foreign key '$f_key'";
07037f89 33 } else {
34 $class =~ /([^\:]+)$/;
dcf8330b 35 $f_key = lc $1; # go ahead and guess; best we can do
36 $guess = "using our class name '$class' as foreign key";
07037f89 37 }
aeb1bf75 38
39 my $f_class_loaded = eval { $f_class->columns };
303cf522 40 $class->throw_exception(
41 "No such column ${f_key} on foreign class ${f_class} ($guess)"
42 ) if $f_class_loaded && !$f_class->has_column($f_key);
d4daee7b 43
8e04bf91 44 $cond = { "foreign.${f_key}" => "self.${pri}" };
07037f89 45 }
46
303cf522 47 $class->add_relationship($rel, $f_class, $cond, {
48 accessor => 'multi',
49 join_type => 'LEFT',
50 cascade_delete => 1,
51 cascade_copy => 1,
52 %{$attrs||{}}
53 });
07037f89 54}
55
561;