Do not find references for empty values.
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 12 Oct 2021 00:17:57 +0000 (13:17 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 12 Oct 2021 01:33:33 +0000 (14:33 +1300)
CRM/Core/Reference/Basic.php

index ae8684837bad0d3adba5e09bde87966316b3e9b8..5bd6792bc855ed4a383accfebefc6abdd2fa3cf9 100644 (file)
@@ -112,21 +112,26 @@ EOS;
    */
   public function getReferenceCount($targetDao) {
     $targetColumn = $this->getTargetKey();
-    $params = [
-      1 => [$targetDao->$targetColumn, 'String'],
-    ];
-    $sql = <<<EOS
+    $count = 0;
+    if ($targetDao->{$targetColumn} !== '' && $targetDao->{$targetColumn} !== NULL) {
+
+      $params = [
+        1 => [$targetDao->{$targetColumn} ?? '', 'String'],
+      ];
+      $sql = <<<EOS
 SELECT count(*)
 FROM {$this->getReferenceTable()}
 WHERE {$this->getReferenceKey()} = %1
 EOS;
+      $count = CRM_Core_DAO::singleValueQuery($sql, $params);
+    }
 
     return [
       'name' => implode(':', ['sql', $this->getReferenceTable(), $this->getReferenceKey()]),
       'type' => get_class($this),
       'table' => $this->getReferenceTable(),
       'key' => $this->getReferenceKey(),
-      'count' => CRM_Core_DAO::singleValueQuery($sql, $params),
+      'count' => $count,
     ];
   }