Introduce 'any_null_means_no_value' option to eliminate wasteful queries. The option...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / Accessor.pm
index fb15f10..f23ec4c 100644 (file)
@@ -18,6 +18,7 @@ sub add_relationship_accessor {
   my ($class, $rel, $acc_type) = @_;
   my %meth;
   if ($acc_type eq 'single') {
+    my $rel_info = $class->relationship_info($rel);
     $meth{$rel} = sub {
       my $self = shift;
       if (@_) {
@@ -26,6 +27,13 @@ sub add_relationship_accessor {
       } elsif (exists $self->{_relationship_data}{$rel}) {
         return $self->{_relationship_data}{$rel};
       } else {
+        my $cond = $self->result_source->resolve_condition(
+          $rel_info->{cond}, $rel, $self
+        );
+        if( exists $rel_info->{attrs}->{any_null_means_no_value}
+              && $rel_info->{attrs}->{any_null_means_no_value} ){
+          return if grep { not defined } values %$cond;
+        }
         my $val = $self->find_related($rel, {}, {});
         return unless $val;
         return $self->{_relationship_data}{$rel} = $val;