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