Commit | Line | Data |
c0e7b4e5 |
1 | package # hide from PAUSE |
2 | DBIx::Class::Relationship::CascadeActions; |
4a07648a |
3 | |
bf5ecff9 |
4 | use strict; |
5 | use warnings; |
6 | |
044e70c7 |
7 | our %_pod_inherit_config = |
8 | ( |
9 | class_map => { 'DBIx::Class::Relationship::CascadeActions' => 'DBIx::Class::Relationship' } |
10 | ); |
11 | |
4a07648a |
12 | sub delete { |
13 | my ($self, @rest) = @_; |
147dd158 |
14 | return $self->next::method(@rest) unless ref $self; |
4a07648a |
15 | # I'm just ignoring this for class deletes because hell, the db should |
16 | # be handling this anyway. Assuming we have joins we probably actually |
17 | # *could* do them, but I'd rather not. |
18 | |
147dd158 |
19 | my $ret = $self->next::method(@rest); |
4a07648a |
20 | |
71e65b39 |
21 | my $source = $self->result_source; |
22 | my %rels = map { $_ => $source->relationship_info($_) } $source->relationships; |
4a07648a |
23 | my @cascade = grep { $rels{$_}{attrs}{cascade_delete} } keys %rels; |
24 | foreach my $rel (@cascade) { |
b1a94f98 |
25 | $self->search_related($rel)->delete_all; |
4a07648a |
26 | } |
27 | return $ret; |
28 | } |
29 | |
30 | sub update { |
31 | my ($self, @rest) = @_; |
147dd158 |
32 | return $self->next::method(@rest) unless ref $self; |
4a07648a |
33 | # Because update cascades on a class *really* don't make sense! |
34 | |
147dd158 |
35 | my $ret = $self->next::method(@rest); |
4a07648a |
36 | |
71e65b39 |
37 | my $source = $self->result_source; |
38 | my %rels = map { $_ => $source->relationship_info($_) } $source->relationships; |
4a07648a |
39 | my @cascade = grep { $rels{$_}{attrs}{cascade_update} } keys %rels; |
40 | foreach my $rel (@cascade) { |
8417f5ee |
41 | next if ( |
b82c8a28 |
42 | $rels{$rel}{attrs}{accessor} |
43 | && |
8417f5ee |
44 | $rels{$rel}{attrs}{accessor} eq 'single' |
b82c8a28 |
45 | && |
46 | !exists($self->{_relationship_data}{$rel}) |
8417f5ee |
47 | ); |
b044b5d3 |
48 | $_->update for grep defined, $self->$rel; |
4a07648a |
49 | } |
50 | return $ret; |
51 | } |
52 | |
53 | 1; |