X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FHasMany.pm;h=3f745f6cbfe6aeab5d2ac8eefe4e6148f35d0093;hb=07037f89d4d9bf97c59a2c083de74f669521da47;hp=56c044d62800351cb48bb4a6fd445f8aaa995977;hpb=f85f550e1a6edce9b939b32b604bbe85f1650d39;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/HasMany.pm b/lib/DBIx/Class/CDBICompat/HasMany.pm index 56c044d..3f745f6 100644 --- a/lib/DBIx/Class/CDBICompat/HasMany.pm +++ b/lib/DBIx/Class/CDBICompat/HasMany.pm @@ -6,48 +6,34 @@ use warnings; sub has_many { my ($class, $rel, $f_class, $f_key, $args) = @_; - my $self_key; + my @f_method; if (ref $f_class eq 'ARRAY') { - ($f_class, $self_key) = @$f_class; + ($f_class, @f_method) = @$f_class; } - if (!$self_key || $self_key eq 'id') { - my ($pri, $too_many) = keys %{ $class->_primaries }; - $class->throw( "has_many only works with a single primary key; ${class} has more" ) - if $too_many; - $self_key = $pri; - } - - eval "require $f_class"; - - if (ref $f_key eq 'HASH') { $args = $f_key; undef $f_key; }; + if (ref $f_key eq 'HASH' && !$args) { $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; - #} + $args ||= {}; + if (delete $args->{no_cascade_delete}) { + $args->{cascade_delete} = 0; + } - unless ($f_key) { - #warn join(', ', %{ $f_class->_columns }); - $class =~ /([^\:]+)$/; - #warn $1; - $f_key = lc $1 if $f_class->_columns->{lc $1}; + $class->NEXT::has_many($rel, $f_class, $f_key, $args); + + 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; } - $class->throw( "Unable to resolve foreign key for has_many from ${class} to ${f_class}" ) - unless $f_key; - $class->throw( "No such column ${f_key} on foreign class ${f_class}" ) - unless $f_class->_columns->{$f_key}; - $args ||= {}; - my $cascade = not (ref $args eq 'HASH' && delete $args->{no_cascade_delete}); - $class->add_relationship($rel, $f_class, - { "foreign.${f_key}" => "self.${self_key}" }, - { accessor => 'multi', - join_type => 'LEFT', - ($cascade ? ('cascade_delete' => 1) : ()), - %$args } ); - return 1; } 1;