Remove stray $DB::single
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / HasA.pm
index c4595c6..647674f 100644 (file)
@@ -1,13 +1,14 @@
-package DBIx::Class::CDBICompat::HasA;
+package # hide from PAUSE
+    DBIx::Class::CDBICompat::HasA;
 
 use strict;
 use warnings;
 
 sub has_a {
   my ($self, $col, $f_class, %args) = @_;
-  $self->throw( "No such column ${col}" ) unless $self->_columns->{$col};
-  eval "require $f_class";
-  if ($args{'inflate'} || $args{'deflate'}) {
+  $self->throw_exception( "No such column ${col}" ) unless $self->has_column($col);
+  $self->ensure_class_loaded($f_class);
+  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 +20,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;