Re-fix relcond resolver: revert 5592d633 (in turn partial revert of 03f6d1f7)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource.pm
index c679634..cc865b4 100644 (file)
@@ -1782,26 +1782,51 @@ sub _resolve_relationship_condition {
   my $args = { ref $_[0] eq 'HASH' ? %{ $_[0] } : @_ };
 
   for ( qw( rel_name self_alias foreign_alias ) ) {
-    $self->throw_exception("Mandatory argument '$_' is not a plain string")
+    $self->throw_exception("Mandatory argument '$_' to _resolve_relationship_condition() is not a plain string")
       if !defined $args->{$_} or length ref $args->{$_};
   }
 
-  $self->throw_exception('No practical way to resolve a relationship between two objects')
-    if defined $args->{self_resultobj} and defined $args->{foreign_resultobj};
+  my $exception_rel_id = "relationship '$args->{rel_name}' on source '@{[ $self->source_name ]}'";
 
   my $rel_info = $self->relationship_info($args->{rel_name});
-  #  or $self->throw_exception( "No such relationship '$args->{rel_name}'" );
-
-  $self->throw_exception( "Object '$args->{foreign_resultobj}' must be of class '$rel_info->{class}'" )
-    if defined blessed $args->{foreign_resultobj} and ! $args->{foreign_resultobj}->isa($rel_info->{class});
+  #  or $self->throw_exception( "No such $exception_rel_id" );
 
-  $args->{condition} ||= $rel_info->{cond};
+  $self->throw_exception("No practical way to resolve $exception_rel_id between two objects")
+    if defined $args->{self_resultobj} and defined $args->{foreign_resultobj};
 
   $self->throw_exception( "Argument to infer_values_based_on must be a hash" )
     if exists $args->{infer_values_based_on} and ref $args->{infer_values_based_on} ne 'HASH';
 
   $args->{require_join_free_condition} ||= !!$args->{infer_values_based_on};
 
+  $args->{condition} ||= $rel_info->{cond};
+
+  if (exists $args->{self_resultobj}) {
+    if (defined blessed $args->{self_resultobj}) {
+#      $self->throw_exception( "Object '$args->{self_resultobj}' must be of class '@{[ $self->result_class ]}'" )
+#        unless $args->{self_resultobj}->isa($self->result_class);
+    }
+    else {
+      $args->{self_resultobj} = DBIx::Class::Core->new({
+        -result_source => $self,
+        %{ $args->{self_resultobj}||{} }
+      });
+    }
+  }
+
+  if (exists $args->{foreign_resultobj}) {
+    if (defined blessed $args->{foreign_resultobj}) {
+#      $self->throw_exception( "Object '$args->{foreign_resultobj}' must be of class '$rel_info->{class}'" )
+#        unless $args->{foreign_resultobj}->isa($rel_info->{class});
+    }
+    else {
+      $args->{foreign_resultobj} = DBIx::Class::Core->new({
+        -result_source => $self->related_source($args->{rel_name}),
+        %{ $args->{foreign_resultobj}||{} }
+      });
+    }
+  }
+
   my $ret;
 
   if (ref $args->{condition} eq 'CODE') {
@@ -1832,7 +1857,7 @@ sub _resolve_relationship_condition {
     if (my $jfc = $ret->{join_free_condition}) {
 
       $self->throw_exception (
-        "The join-free condition returned for relationship '$args->{rel_name}' must be a hash reference"
+        "The join-free condition returned for $exception_rel_id must be a hash reference"
       ) unless ref $jfc eq 'HASH';
 
       my ($joinfree_alias, $joinfree_source);
@@ -1847,7 +1872,7 @@ sub _resolve_relationship_condition {
 
       # FIXME sanity check until things stabilize, remove at some point
       $self->throw_exception (
-        "A join-free condition returned for relationship '$args->{rel_name}' without a result object to chain from"
+        "A join-free condition returned for $exception_rel_id without a result object to chain from"
       ) unless $joinfree_alias;
 
       my $fq_col_list = { map
@@ -1856,7 +1881,7 @@ sub _resolve_relationship_condition {
       };
 
       $fq_col_list->{$_} or $self->throw_exception (
-        "The join-free condition returned for relationship '$args->{rel_name}' may only "
+        "The join-free condition returned for $exception_rel_id may only "
       . 'contain keys that are fully qualified column names of the corresponding source'
       ) for keys %$jfc;
 
@@ -1897,11 +1922,7 @@ sub _resolve_relationship_condition {
 
       for my $i (0..$#$obj_cols) {
 
-        # FIXME - temp shim
-        if (! blessed $obj) {
-          $ret->{join_free_condition}{"$plain_alias.$plain_cols->[$i]"} = $obj->{$obj_cols->[$i]};
-        }
-        elsif (
+        if (
           defined $args->{self_resultobj}
             and
           ! $obj->has_column_loaded($obj_cols->[$i])
@@ -1956,10 +1977,10 @@ sub _resolve_relationship_condition {
     }
   }
   else {
-    $self->throw_exception ("Can't handle condition $args->{condition} for relationship '$args->{rel_name}' yet :(");
+    $self->throw_exception ("Can't handle condition $args->{condition} for $exception_rel_id yet :(");
   }
 
-  $self->throw_exception("Relationship '$args->{rel_name}' does not resolve to a join-free condition fragment") if (
+  $self->throw_exception(ucfirst "$exception_rel_id does not resolve to a join-free condition fragment") if (
     $args->{require_join_free_condition}
       and
     ( ! $ret->{join_free_condition} or $ret->{join_free_condition} eq UNRESOLVABLE_CONDITION )
@@ -1994,8 +2015,7 @@ sub _resolve_relationship_condition {
   if ($args->{infer_values_based_on}) {
 
     $self->throw_exception(sprintf (
-      "Unable to complete value inferrence - custom relationship '%s' returns conditions instead of values for column(s): %s",
-      $args->{rel_name},
+      "Unable to complete value inferrence - custom $exception_rel_id returns conditions instead of values for column(s): %s",
       map { "'$_'" } @nonvalues
     )) if @nonvalues;