X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FHasMany.pm;h=382b9cb2b4babf98426f01a8224100465f3f3374;hb=e9188247f020a63ab8b6280c9dcdcb0df5b5f0c1;hp=f96cf9c5b4344763ab90c5c971e322af41f57fd7;hpb=604d9f388716261ca478b574f891928e8e0852ef;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/HasMany.pm b/lib/DBIx/Class/CDBICompat/HasMany.pm index f96cf9c..382b9cb 100644 --- a/lib/DBIx/Class/CDBICompat/HasMany.pm +++ b/lib/DBIx/Class/CDBICompat/HasMany.pm @@ -1,56 +1,40 @@ -package DBIx::Class::CDBICompat::HasMany; +package # hide from PAUSE + DBIx::Class::CDBICompat::HasMany; use strict; use warnings; sub has_many { my ($class, $rel, $f_class, $f_key, $args) = @_; - #die "No such column ${col}" unless $class->_columns->{$col}; - eval "require $f_class"; - my ($pri, $too_many) = keys %{ $class->_primaries }; - die "has_many only works with a single primary key; ${class} has more" - if $too_many; - if (ref $f_key eq 'HASH') { $args = $f_key; undef $f_key; }; - #unless ($f_key) { Not selective enough. Removed pending fix. - # ($f_rel) = grep { $_->{class} && $_->{class} eq $class } - # $f_class->_relationships; - #} - unless ($f_key) { - #warn join(', ', %{ $f_class->_columns }); - $class =~ /([^\:]+)$/; - #warn $1; - $f_key = lc $1 if $f_class->_columns->{lc $1}; - } - die "Unable to resolve foreign key for has_many from ${class} to ${f_class}" - unless $f_key; - die "No such column ${f_key} on foreign class ${f_class}" - unless $f_class->_columns->{$f_key}; - $class->add_relationship($rel, $f_class, - { "foreign.${f_key}" => "self.${pri}" }, - { _type => 'has_many', %{$args || {}} } ); - { - no strict 'refs'; - *{"${class}::${rel}"} = sub { shift->search_related($rel, @_); }; - *{"${class}::add_to_${rel}"} = sub { shift->create_related($rel, @_); }; + + my @f_method; + + if (ref $f_class eq 'ARRAY') { + ($f_class, @f_method) = @$f_class; } - return 1; -} -sub delete { - my ($self, @rest) = @_; - return $self->NEXT::ACTUAL::delete(@rest) unless ref $self; - # I'm just ignoring this for class deletes because hell, the db should - # be handling this anyway. Assuming we have joins we probably actually - # *could* do them, but I'd rather not. + if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; }; - my $ret = $self->NEXT::ACTUAL::delete(@rest); + $args ||= {}; + if (delete $args->{no_cascade_delete}) { + $args->{cascade_delete} = 0; + } + + $class->next::method($rel, $f_class, $f_key, $args); - my %rels = %{ $self->_relationships }; - my @hm = grep { $rels{$_}{attrs}{_type} - && $rels{$_}{attrs}{_type} eq 'has_many' } keys %rels; - foreach my $has_many (@hm) { - $_->delete for $self->search_related($has_many); + if (@f_method) { + no strict 'refs'; + no warnings 'redefine'; + my $post_proc = sub { my $o = shift; $o = $o->$_ for @f_method; $o; }; + *{"${class}::${rel}"} = + sub { + my $rs = shift->search_related($rel => @_); + $rs->{attrs}{record_filter} = $post_proc; + return (wantarray ? $rs->all : $rs); + }; + return 1; } - return $ret; + } + 1;