Commit | Line | Data |
c0e7b4e5 |
1 | package # hide from PAUSE |
2 | DBIx::Class::CDBICompat::HasMany; |
b8e1e21f |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | sub has_many { |
8 | my ($class, $rel, $f_class, $f_key, $args) = @_; |
95a70f01 |
9 | |
33ce49d6 |
10 | my @f_method; |
95a70f01 |
11 | |
12 | if (ref $f_class eq 'ARRAY') { |
33ce49d6 |
13 | ($f_class, @f_method) = @$f_class; |
95a70f01 |
14 | } |
15 | |
07037f89 |
16 | if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; }; |
95a70f01 |
17 | |
07037f89 |
18 | $args ||= {}; |
19 | if (delete $args->{no_cascade_delete}) { |
20 | $args->{cascade_delete} = 0; |
9bc6db13 |
21 | } |
95a70f01 |
22 | |
3bec1f52 |
23 | if( !$f_key and !@f_method ) { |
24 | my $f_source = $f_class->result_source_instance; |
25 | ($f_key) = grep { $f_source->relationship_info($_)->{class} eq $class } |
26 | $f_source->relationships; |
27 | } |
28 | |
147dd158 |
29 | $class->next::method($rel, $f_class, $f_key, $args); |
33ce49d6 |
30 | |
33ce49d6 |
31 | if (@f_method) { |
32 | no strict 'refs'; |
33 | no warnings 'redefine'; |
34 | my $post_proc = sub { my $o = shift; $o = $o->$_ for @f_method; $o; }; |
35 | *{"${class}::${rel}"} = |
36 | sub { |
37 | my $rs = shift->search_related($rel => @_); |
38 | $rs->{attrs}{record_filter} = $post_proc; |
39 | return (wantarray ? $rs->all : $rs); |
40 | }; |
41 | return 1; |
42 | } |
43 | |
b8e1e21f |
44 | } |
45 | |
b8e1e21f |
46 | 1; |