Added ->relationships and ->relationship_info from castaway
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / CascadeActions.pm
1 package DBIx::Class::Relationship::CascadeActions;
2
3 sub delete {
4   my ($self, @rest) = @_;
5   return $self->next::method(@rest) unless ref $self;
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
10   my $ret = $self->next::method(@rest);
11
12   my %rels = map { $_ => $self->relationship_info($_) } $self->relationships;
13   my @cascade = grep { $rels{$_}{attrs}{cascade_delete} } keys %rels;
14   foreach my $rel (@cascade) {
15     $self->search_related($rel)->delete;
16   }
17   return $ret;
18 }
19
20 sub update {
21   my ($self, @rest) = @_;
22   return $self->next::method(@rest) unless ref $self;
23     # Because update cascades on a class *really* don't make sense!
24
25   my $ret = $self->next::method(@rest);
26
27   my %rels = map { $_ => $self->relationship_info($_) } $self->relationships;
28   my @cascade = grep { $rels{$_}{attrs}{cascade_update} } keys %rels;
29   foreach my $rel (@cascade) {
30     $_->update for $self->$rel;
31   }
32   return $ret;
33 }
34
35 1;