Clarification cascade_* attribute defaults documentation
[dbsrgits/DBIx-Class-Historic.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);
e8fb771b 17 my ($pri, $too_many) = eval { $class->_pri_cols };
18 $class->throw_exception(
19 "Can't infer join condition for ${rel} on ${class}: $@"
20 ) if $@;
fd4df975 21
22 $class->throw_exception(
23 "has_many can only infer join for a single primary key; ".
24 "${class} has more"
25 ) if $too_many;
aeb1bf75 26
e36de82e 27 $class->throw_exception(
28 "has_many needs a primary key to infer a join; ".
29 "${class} has none"
30 ) if !defined $pri && (!defined $cond || !length $cond);
31
aeb1bf75 32 my ($f_key,$guess);
07037f89 33 if (defined $cond && length $cond) {
34 $f_key = $cond;
dcf8330b 35 $guess = "caller specified foreign key '$f_key'";
07037f89 36 } else {
37 $class =~ /([^\:]+)$/;
dcf8330b 38 $f_key = lc $1; # go ahead and guess; best we can do
39 $guess = "using our class name '$class' as foreign key";
07037f89 40 }
aeb1bf75 41
42 my $f_class_loaded = eval { $f_class->columns };
303cf522 43 $class->throw_exception(
44 "No such column ${f_key} on foreign class ${f_class} ($guess)"
45 ) if $f_class_loaded && !$f_class->has_column($f_key);
d4daee7b 46
8e04bf91 47 $cond = { "foreign.${f_key}" => "self.${pri}" };
07037f89 48 }
49
303cf522 50 $class->add_relationship($rel, $f_class, $cond, {
51 accessor => 'multi',
52 join_type => 'LEFT',
53 cascade_delete => 1,
54 cascade_copy => 1,
55 %{$attrs||{}}
56 });
07037f89 57}
58
591;