CRM-21110 Get Total relationships without need for extra table
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 21 Sep 2017 00:42:36 +0000 (10:42 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 29 Sep 2017 23:29:16 +0000 (09:29 +1000)
CRM/Contact/BAO/Relationship.php

index 64edc3a979f19e424e77a0ed5fbc0f1b4c1f65c8..b2ac947046f22549570420cd30985cc0545ef4e8 100644 (file)
@@ -1279,25 +1279,23 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
     }
 
     // building the query string
-    CRM_Core_DAO::executeQuery("CREATE TEMPORARY TABLE civicrm_contact_relationships " . $select1 . $from1 . $where1 . $select2 . $from2 . $where2);
-    $queryString = "SELECT * FROM civicrm_contact_relationships " . $order . $limit;
+    $queryString = $select1 . $from1 . $where1 . $select2 . $from2 . $where2;
 
     $relationship = new CRM_Contact_DAO_Relationship();
 
-    $relationship->query($queryString);
+    $relationship->query($queryString . $order . $limit);
     $row = array();
     if ($count) {
       $relationshipCount = 0;
       while ($relationship->fetch()) {
         $relationshipCount += $relationship->cnt1 + $relationship->cnt2;
       }
-      CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS civicrm_contact_relationships");
       return $relationshipCount;
     }
     else {
 
       if ($includeTotalCount) {
-        $values['total_relationships'] = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_contact_relationships");
+        $values['total_relationships'] = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM ({$queryString}) AS r");
       }
 
       $mask = NULL;
@@ -1445,7 +1443,6 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
       }
 
       $relationship->free();
-      CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS civicrm_contact_relationships");
       return $values;
     }
   }