Addition of a bunch of helper relationship methods
[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
11   if (!ref $cond) {
12     my $f_key;
13     if (defined $cond && length $cond) {
14       $f_key = $cond;
15       $class->throw( "No such column ${f_key} on foreign class ${f_class}" )
16         unless ($@ || $f_class->_columns->{$f_key});
17     } else {
18       $class =~ /([^\:]+)$/;
19       $f_key = lc $1 if $f_class->_columns->{lc $1};
20       $class->throw( "Unable to resolve foreign key for has_many from ${class} to ${f_class}" )
21         unless $f_key;
22     }
23     my ($pri, $too_many) = keys %{ $class->_primaries };
24     $class->throw( "has_many can only infer join for a single primary key; ${class} has more" )
25       if $too_many;
26     $cond = { "foreign.${f_key}" => "self.${pri}" },
27   }
28
29   $class->add_relationship($rel, $f_class, $cond,
30                             { accessor => 'multi',
31                               join_type => 'LEFT',
32                               cascade_delete => 1,
33                               %{$attrs||{}} } );
34 }
35
36 1;