Shoved Class::Data::Inheritable into DBIx::Class, more stuff works now
[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   unless (ref $cond) {
12     my ($pri, $too_many) = $class->primary_columns;
13     $class->throw( "has_many can only infer join for a single primary key; ${class} has more" )
14       if $too_many;
15     my $f_key;
16     my $f_class_loaded = eval { $f_class->columns };
17     my $guess;
18     if (defined $cond && length $cond) {
19       $f_key = $cond;
20       $guess = "caller specified foreign key '$f_key'";
21     } else {
22       $class =~ /([^\:]+)$/;
23       $f_key = lc $1; # go ahead and guess; best we can do
24       $guess = "using our class name '$class' as foreign key";
25     }
26     $class->throw("No such column ${f_key} on foreign class ${f_class} ($guess)")
27       if $f_class_loaded && !$f_class->has_column($f_key);
28     $cond = { "foreign.${f_key}" => "self.${pri}" },
29   }
30
31   $class->add_relationship($rel, $f_class, $cond,
32                             { accessor => 'multi',
33                               join_type => 'LEFT',
34                               cascade_delete => 1,
35                               %{$attrs||{}} } );
36 }
37
38 1;