Add txn_scope_guard method/object
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / HasMany.pm
1 package # hide from PAUSE
2     DBIx::Class::CDBICompat::HasMany;
3
4 use strict;
5 use warnings;
6
7 sub has_many {
8   my ($class, $rel, $f_class, $f_key, $args) = @_;
9
10   my @f_method;
11
12   if (ref $f_class eq 'ARRAY') {
13     ($f_class, @f_method) = @$f_class;
14   }
15
16   if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; };
17
18   $args ||= {};
19   if (delete $args->{no_cascade_delete}) {
20     $args->{cascade_delete} = 0;
21   }
22
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
29   $class->next::method($rel, $f_class, $f_key, $args);
30
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
44 }
45
46 1;