in reply to Class::DBI many to many delete

It should work. According to the docs, the default for cascading delete is to delete the child if the parent is deleted; but it's not working here. So, we have to work around it.

Class::DBI doesn't support Many to Many right now. The module's author does say that you can manually set up the relationships and get some measure of control by writing a Class::DBI::Cascade::Plugin::Nullify and inserting it into the relationship:

WebR::M::CDBI::Datatype->has_many(children=>['WebR::M::CDBI::DatatypeR +elationship'=>'child"], 'parent', { cascade => 'Class::DBI::Cascade::Plugin::Nullify' });

Try it and see if that works. I hope this helps.

Replies are listed 'Best First'.
Re^2: Class::DBI many to many delete
by CassJ (Sexton) on Jul 18, 2006 at 00:12 UTC
    Odd. This works fine:
    WebR::M::CDBI::DatafileRelationship->has_a(child=>'WebR::M::CDBI:: +Datafile'); WebR::M::CDBI::DatafileRelationship->has_a(parent=>'WebR::M::CDBI: +:Datafile'); WebR::M::CDBI::Datafile->has_many(parents=>['WebR::M::CDBI::Datafi +leRelationship' => 'parent'],'child', { cascade => 'Class::DBI::Cascade::Delete'}); WebR::M::CDBI::Datafile->has_many(children=>['WebR::M::CDBI::Dataf +ileRelationship' => 'child'],'parent', { cascade => 'Class::DBI::Cascade::Delete'} );
    Ah - and if I do
    WebR::M::CDBI::DatafileRelationship->has_a(child=>'WebR::M::CDBI:: +Datafile', {cascade=>'Class::DBI::Cascade::Delete'});
    then the child datafiles get deleted too. Great! thanks! And, for the has_many relationships where I just want the FK to be nulled, rather than the record deleted:
    package Class::DBI::Cascade::Nullify; use base 'Class::DBI::Cascade::None'; use Data::Dumper; sub cascade { my ($self, $obj) = @_; my $fk =$self->{_rel}->args->{foreign_key}; foreach ($self->foreign_for($obj)){ $_->$fk(undef); $_->update; } } 1;