Commit | Line | Data |
75d07914 |
1 | package # hide from PAUSE |
c0e7b4e5 |
2 | DBIx::Class::Relationship::HasMany; |
07037f89 |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | sub has_many { |
8 | my ($class, $rel, $f_class, $cond, $attrs) = @_; |
c037c03a |
9 | |
dcf8330b |
10 | unless (ref $cond) { |
fd4df975 |
11 | $class->ensure_class_loaded($f_class); |
103647d5 |
12 | my ($pri, $too_many) = $class->primary_columns; |
fd4df975 |
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; |
aeb1bf75 |
18 | |
19 | my ($f_key,$guess); |
07037f89 |
20 | if (defined $cond && length $cond) { |
21 | $f_key = $cond; |
dcf8330b |
22 | $guess = "caller specified foreign key '$f_key'"; |
07037f89 |
23 | } else { |
24 | $class =~ /([^\:]+)$/; |
dcf8330b |
25 | $f_key = lc $1; # go ahead and guess; best we can do |
26 | $guess = "using our class name '$class' as foreign key"; |
07037f89 |
27 | } |
aeb1bf75 |
28 | |
29 | my $f_class_loaded = eval { $f_class->columns }; |
303cf522 |
30 | $class->throw_exception( |
31 | "No such column ${f_key} on foreign class ${f_class} ($guess)" |
32 | ) if $f_class_loaded && !$f_class->has_column($f_key); |
aeb1bf75 |
33 | |
8e04bf91 |
34 | $cond = { "foreign.${f_key}" => "self.${pri}" }; |
07037f89 |
35 | } |
36 | |
303cf522 |
37 | $class->add_relationship($rel, $f_class, $cond, { |
38 | accessor => 'multi', |
39 | join_type => 'LEFT', |
40 | cascade_delete => 1, |
41 | cascade_copy => 1, |
42 | %{$attrs||{}} |
43 | }); |
07037f89 |
44 | } |
45 | |
46 | 1; |