[Dbix-class] Cascade deletes

Peter Rabbitson rabbit+dbic at rabbit.us
Wed Mar 10 19:38:45 GMT 2010


Pedro Melo wrote:
> Hi,
> 
> On Tue, Mar 9, 2010 at 10:23 PM, Bill Moseley <moseley at hank.org> wrote:
>> On Sat, Mar 6, 2010 at 1:24 AM, Pedro Melo <melo at simplicidade.org> wrote:
>>> I did that once for exactly the same reasons. Created a DBIC component
>>> that overrides add_relationship(). The fourth parameter is the hashref
>>> with the options, set cascade_delete => 0 and call the next method.
>> I just added this to my Result base class.  Look similar to what you are
>> doing?  True, a component is probably a better long-term solution.
> 
> It was client work, I don't have access to the code anymore. But it
> looks good. See below for two improvements.
> 
>> sub add_relationship {
>>     my ( $self, @rest ) = @_;
>>     my $options = $rest[3];
>>     if ( ref $options eq 'HASH' ) {
>>         for ( qw/ cascade_delete cascade_copy / ) {
>>             $options->{$_} = 0 if $options->{$_};
>>         }
>>     }
>>     return $self->next::method( @rest );
>> }
> 
> sub add_relationship {
>     my ( $self, @rest ) = @_;
> 
>     # Make sure we have options
>     # If we want to force cascade_* stuff
>     my $options = $rest[3] ||= {};
>     for ( qw/ cascade_delete cascade_copy / ) {
>         # Any user-defined value wins
>         $options->{$_} = 0 unless exists $options->{$_};
>     }
> 
>     return $self->next::method( @rest );
> }
> 

As you already realized down the thread, this is the wrong place to
add a default. It's the individual cascade-setting helper that you need to
override:

sub has_many {
  my ($self, @args) = @_;
  $args[3] = { %your_defaults, %{$args[3]||{}} };
  $self->next::method(@args);
}




More information about the DBIx-Class mailing list