DBIx-Class-DeleteAction

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    'delete_action' key to the optional attribute HASH reference when
    specifing a new relation (see DBIx::Class::Relationship).

    The following delete actions are supported:

    *   null

        Set all columns in related rows pointing to this record to NULL.
        Only works on 'has_many' relationships.

    *   delete OR cascade

        Delete all related records one by one. This can trigger further
        delete actions.

    *   deleteall

        Delete all related records in a single step. This does not trigger
        further delete actions.

        Only works on 'has_many' relationships.

README  view on Meta::CPAN

     );

  Debugging
    Use "DBIC_TRACE=1" or set "__PACKAGE__-"storage->debug(1);> to see what
    is exactly going on.

CAVEATS
    Note that the "delete" method in DBIx::Class::ResultSet will not run
    DeleteAction triggers. See "delete_all" if you need triggers to run.

    Any database-level cascade, restrict or trigger will be performed AFTER
    DBIx-Class-DeleteAction based triggers.

    Always use transactions, or else you might end up with inconsistent
    data.

SUPPORT
    Please report any bugs or feature requests to
    bug-dbix-class-deleteaction@rt.cpan.org, or through the web interface at
    <http://rt.cpan.org/Public/Bug/Report.html?Queue=DBIx::Class::DeleteActi
    on>. I will be notified, and then you'll automatically be notified of

README.mkdn  view on Meta::CPAN

'delete_action' key to the optional attribute HASH reference when specifing
a new relation (see [DBIx::Class::Relationship](http://search.cpan.org/search?mode=module&query=DBIx::Class::Relationship)).

The following delete actions are supported:

- * null

Set all columns in related rows pointing to this record to NULL. Only works
on 'has_many' relationships.

- * delete OR cascade

Delete all related records one by one. This can trigger further delete 
actions.

- * deleteall

Delete all related records in a single step. This does not trigger further 
delete actions.

Only works on 'has_many' relationships.

README.mkdn  view on Meta::CPAN

## Debugging

Use `DBIC_TRACE=1` or set `__PACKAGE__-`storage->debug(1);> to see what
is exactly going on.

# CAVEATS

Note that the `delete` method in [DBIx::Class::ResultSet](http://search.cpan.org/search?mode=module&query=DBIx::Class::ResultSet) will not run 
DeleteAction triggers. See `delete_all` if you need triggers to run.

Any database-level cascade, restrict or trigger will be performed AFTER 
DBIx-Class-DeleteAction based triggers.

Always use transactions, or else you might end up with inconsistent data.

# SUPPORT

Please report any bugs or feature requests to 
L<bug-dbix-class-deleteaction@rt.cpan.org>, or through the web interface at
<http://rt.cpan.org/Public/Bug/Report.html?Queue=DBIx::Class::DeleteAction>.
I will be notified, and then you'll automatically be notified of progress on 

lib/DBIx/Class/DeleteAction.pm  view on Meta::CPAN


The following delete actions are supported:

=over

=item * null

Set all columns in related rows pointing to this record to NULL. Only works
on 'has_many' relationships.

=item * delete OR cascade

Delete all related records one by one. This can trigger further delete 
actions.

=item * deleteall

Delete all related records in a single step. This does not trigger further 
delete actions.

Only works on 'has_many' relationships.

lib/DBIx/Class/DeleteAction.pm  view on Meta::CPAN

    my $source = $self->result_source;

    # Loop all relations
    RELATIONSHIP: foreach my $relationship ($source->relationships) {
        my $relationship_info = $source->relationship_info($relationship);
         
        # Ignore relation with no 'delete_action' key set
        next RELATIONSHIP
            unless $relationship_info->{attrs}{delete_action};
         
        # Unset DBIC key cascade_delete attribute, so that we do not
        # work twice
        $relationship_info->{attrs}{cascade_delete} = 0;
          
        # Get delete action parameter value
        my $delete_action = $relationship_info->{attrs}{delete_action};

        next RELATIONSHIP 
            if $delete_action eq 'ignore';
        
        my $related;
        # Only get relations with data
        if ($relationship_info->{attrs}{accessor} eq 'multi') {

lib/DBIx/Class/DeleteAction.pm  view on Meta::CPAN

                foreach my $key (keys %{$relationship_info->{cond}} ) {
                    next RELATIONSHIP
                        unless $key =~ /^foreign\.(.+)$/;
                    $update->{$1} = undef;    
                }
                $related->update($update);
            } else {
                warn("Delete action 'null' does not work with ".$relationship_info->{attrs}{accessor}." relations");
            }
        # Action: DELETE
        } elsif ($delete_action eq 'delete' || $delete_action eq 'cascade') {
            warn('DeleteAction: DELETE '.$self.'->'.$relationship) if $debug;
            if ($related->isa('DBIx::Class::ResultSet')) {
                while (my $item = $related->next) {
                    $item->delete($params);
                }
            } else {
                $related->delete($params);
            }
        # Action: DELETEALL
        } elsif ($delete_action eq 'deleteall') {

lib/DBIx/Class/DeleteAction.pm  view on Meta::CPAN

=head2 Debugging

Use C<DBIC_TRACE=1> or set C<__PACKAGE__->storage->debug(1);> to see what
is exactly going on.

=head1 CAVEATS

Note that the C<delete> method in L<DBIx::Class::ResultSet> will not run 
DeleteAction triggers. See C<delete_all> if you need triggers to run.

Any database-level cascade, restrict or trigger will be performed AFTER 
DBIx-Class-DeleteAction based triggers.

Always use transactions, or else you might end up with inconsistent data.

=head1 SUPPORT

Please report any bugs or feature requests to 
L<bug-dbix-class-deleteaction@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/Public/Bug/Report.html?Queue=DBIx::Class::DeleteAction>.
I will be notified, and then you'll automatically be notified of progress on 

t/lib/DATest/Schema/Test2A.pm  view on Meta::CPAN

    data_type => "integer",
    is_nullable => 1,
  },
);
__PACKAGE__->set_primary_key('id');   

__PACKAGE__->belongs_to(
    'b' => 'DATest::Schema::Test2B', 
    { 'foreign.id'  => 'self.b' },
    { 
        delete_action   => 'cascade',
    }
);
   
1;



( run in 1.100 second using v1.01-cache-2.11-cpan-49f99fa48dc )