Corrected columns_info_for indent level - 2 space indent everyone please, you may...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / HasMany.pm
CommitLineData
c0e7b4e5 1package # hide from PAUSE
2 DBIx::Class::Relationship::HasMany;
07037f89 3
4use strict;
5use warnings;
6
7sub has_many {
8 my ($class, $rel, $f_class, $cond, $attrs) = @_;
9
10 eval "require $f_class";
55de06f1 11 if ($@) {
701da8c4 12 $class->throw_exception($@) unless $@ =~ /Can't locate/;
55de06f1 13 }
07037f89 14
dcf8330b 15 unless (ref $cond) {
103647d5 16 my ($pri, $too_many) = $class->primary_columns;
701da8c4 17 $class->throw_exception( "has_many can only infer join for a single primary key; ${class} has more" )
dcf8330b 18 if $too_many;
aeb1bf75 19
20 my ($f_key,$guess);
07037f89 21 if (defined $cond && length $cond) {
22 $f_key = $cond;
dcf8330b 23 $guess = "caller specified foreign key '$f_key'";
07037f89 24 } else {
25 $class =~ /([^\:]+)$/;
dcf8330b 26 $f_key = lc $1; # go ahead and guess; best we can do
27 $guess = "using our class name '$class' as foreign key";
07037f89 28 }
aeb1bf75 29
30 my $f_class_loaded = eval { $f_class->columns };
701da8c4 31 $class->throw_exception("No such column ${f_key} on foreign class ${f_class} ($guess)")
103647d5 32 if $f_class_loaded && !$f_class->has_column($f_key);
aeb1bf75 33
8e04bf91 34 $cond = { "foreign.${f_key}" => "self.${pri}" };
07037f89 35 }
36
37 $class->add_relationship($rel, $f_class, $cond,
38 { accessor => 'multi',
39 join_type => 'LEFT',
40 cascade_delete => 1,
333cce60 41 cascade_copy => 1,
07037f89 42 %{$attrs||{}} } );
43}
44
451;