Commit | Line | Data |
4a07648a |
1 | package DBIx::Class::Relationship::CascadeActions; |
2 | |
3 | sub delete { |
4 | my ($self, @rest) = @_; |
147dd158 |
5 | return $self->next::method(@rest) unless ref $self; |
4a07648a |
6 | # I'm just ignoring this for class deletes because hell, the db should |
7 | # be handling this anyway. Assuming we have joins we probably actually |
8 | # *could* do them, but I'd rather not. |
9 | |
147dd158 |
10 | my $ret = $self->next::method(@rest); |
4a07648a |
11 | |
71e65b39 |
12 | my $source = $self->result_source; |
13 | my %rels = map { $_ => $source->relationship_info($_) } $source->relationships; |
4a07648a |
14 | my @cascade = grep { $rels{$_}{attrs}{cascade_delete} } keys %rels; |
15 | foreach my $rel (@cascade) { |
503536d5 |
16 | $self->search_related($rel)->delete; |
4a07648a |
17 | } |
18 | return $ret; |
19 | } |
20 | |
21 | sub update { |
22 | my ($self, @rest) = @_; |
147dd158 |
23 | return $self->next::method(@rest) unless ref $self; |
4a07648a |
24 | # Because update cascades on a class *really* don't make sense! |
25 | |
147dd158 |
26 | my $ret = $self->next::method(@rest); |
4a07648a |
27 | |
71e65b39 |
28 | my $source = $self->result_source; |
29 | my %rels = map { $_ => $source->relationship_info($_) } $source->relationships; |
4a07648a |
30 | my @cascade = grep { $rels{$_}{attrs}{cascade_update} } keys %rels; |
31 | foreach my $rel (@cascade) { |
32 | $_->update for $self->$rel; |
33 | } |
34 | return $ret; |
35 | } |
36 | |
37 | 1; |