X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FHasA.pm;h=c0d445847931eebbbc340ee7f970785db3e0ef6f;hb=39e54ad026643adc23699f2e187c0e71ec4b6532;hp=c4595c63b73a816f023d0cd253fc1d72a7d3a851;hpb=126042ee4b9394c4eecc6ece49469da6fce23ba3;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/HasA.pm b/lib/DBIx/Class/CDBICompat/HasA.pm index c4595c6..c0d4458 100644 --- a/lib/DBIx/Class/CDBICompat/HasA.pm +++ b/lib/DBIx/Class/CDBICompat/HasA.pm @@ -5,9 +5,9 @@ use warnings; sub has_a { my ($self, $col, $f_class, %args) = @_; - $self->throw( "No such column ${col}" ) unless $self->_columns->{$col}; + $self->throw( "No such column ${col}" ) unless $self->has_column($col); eval "require $f_class"; - if ($args{'inflate'} || $args{'deflate'}) { + if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a if (!ref $args{'inflate'}) { my $meth = $args{'inflate'}; $args{'inflate'} = sub { $f_class->$meth(shift); }; @@ -19,13 +19,27 @@ sub has_a { $self->inflate_column($col, \%args); return 1; } - my ($pri, $too_many) = keys %{ $f_class->_primaries }; - $self->throw( "has_a only works with a single primary key; ${f_class} has more" ) - if $too_many; - $self->add_relationship($col, $f_class, - { "foreign.${pri}" => "self.${col}" }, - { accessor => 'filter' } ); + + $self->belongs_to($col, $f_class); return 1; } +sub search { + my $self = shift; + my $attrs = {}; + if (@_ > 1 && ref $_[$#_] eq 'HASH') { + $attrs = { %{ pop(@_) } }; + } + my $where = (@_ ? ((@_ == 1) ? ((ref $_[0] eq "HASH") ? { %{+shift} } : shift) + : {@_}) + : undef()); + if (ref $where eq 'HASH') { + foreach my $key (keys %$where) { # has_a deflation hack + $where->{$key} = ''.$where->{$key} + if eval { $where->{$key}->isa('DBIx::Class') }; + } + } + $self->next::method($where, $attrs); +} + 1;