Commit | Line | Data |
c0e7b4e5 |
1 | package # hide from PAUSE |
2 | DBIx::Class::Relationship::CascadeActions; |
4a07648a |
3 | |
bf5ecff9 |
4 | use strict; |
5 | use warnings; |
70c28808 |
6 | use DBIx::Class::Carp; |
bf5ecff9 |
7 | |
044e70c7 |
8 | our %_pod_inherit_config = |
9 | ( |
10 | class_map => { 'DBIx::Class::Relationship::CascadeActions' => 'DBIx::Class::Relationship' } |
11 | ); |
12 | |
4a07648a |
13 | sub delete { |
14 | my ($self, @rest) = @_; |
147dd158 |
15 | return $self->next::method(@rest) unless ref $self; |
4a07648a |
16 | # I'm just ignoring this for class deletes because hell, the db should |
17 | # be handling this anyway. Assuming we have joins we probably actually |
18 | # *could* do them, but I'd rather not. |
19 | |
71e65b39 |
20 | my $source = $self->result_source; |
21 | my %rels = map { $_ => $source->relationship_info($_) } $source->relationships; |
4a07648a |
22 | my @cascade = grep { $rels{$_}{attrs}{cascade_delete} } keys %rels; |
fb61e30c |
23 | |
24 | if (@cascade) { |
25 | my $guard = $source->schema->txn_scope_guard; |
26 | |
27 | my $ret = $self->next::method(@rest); |
28 | |
29 | foreach my $rel (@cascade) { |
51c9ead2 |
30 | if( my $rel_rs = eval{ $self->search_related($rel) } ) { |
31 | $rel_rs->delete_all; |
32 | } else { |
33 | carp "Skipping cascade delete on relationship '$rel' - related resultsource '$rels{$rel}{class}' is not registered with this schema"; |
34 | next; |
35 | } |
fb61e30c |
36 | } |
37 | |
38 | $guard->commit; |
39 | return $ret; |
4a07648a |
40 | } |
fb61e30c |
41 | |
42 | $self->next::method(@rest); |
4a07648a |
43 | } |
44 | |
45 | sub update { |
46 | my ($self, @rest) = @_; |
147dd158 |
47 | return $self->next::method(@rest) unless ref $self; |
4a07648a |
48 | # Because update cascades on a class *really* don't make sense! |
49 | |
71e65b39 |
50 | my $source = $self->result_source; |
51 | my %rels = map { $_ => $source->relationship_info($_) } $source->relationships; |
4a07648a |
52 | my @cascade = grep { $rels{$_}{attrs}{cascade_update} } keys %rels; |
fb61e30c |
53 | |
54 | if (@cascade) { |
55 | my $guard = $source->schema->txn_scope_guard; |
56 | |
57 | my $ret = $self->next::method(@rest); |
58 | |
59 | foreach my $rel (@cascade) { |
60 | next if ( |
61 | $rels{$rel}{attrs}{accessor} |
62 | && |
63 | $rels{$rel}{attrs}{accessor} eq 'single' |
64 | && |
65 | !exists($self->{_relationship_data}{$rel}) |
66 | ); |
67 | $_->update for grep defined, $self->$rel; |
68 | } |
69 | |
70 | $guard->commit; |
71 | return $ret; |
4a07648a |
72 | } |
fb61e30c |
73 | |
74 | $self->next::method(@rest); |
4a07648a |
75 | } |
76 | |
77 | 1; |