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