Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21110 Do not run same/similar query twice when generating Relatio… #10907

Merged
merged 2 commits into from
Sep 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,8 @@ public static function makeURLClause($contactId, $status, $numRelationship, $cou
* @param bool $permissionedContact
* to return only permissioned Contact
* @param array $params
* @param bool $includeTotalCount
* Should we return a count of total accessable relationships
*
* @return array|int
* relationship records
Expand All @@ -1243,7 +1245,7 @@ public static function getRelationship(
$count = 0, $relationshipId = 0,
$links = NULL, $permissionMask = NULL,
$permissionedContact = FALSE,
$params = array()
$params = array(), $includeTotalCount = FALSE
) {
$values = array();
if (!$contactId && !$relationshipId) {
Expand Down Expand Up @@ -1277,7 +1279,8 @@ public static function getRelationship(
}

// building the query string
$queryString = $select1 . $from1 . $where1 . $select2 . $from2 . $where2 . $order . $limit;
CRM_Core_DAO::executeQuery("CREATE TEMPORARY TABLE civicrm_contact_relationships " . $select1 . $from1 . $where1 . $select2 . $from2 . $where2);
$queryString = "SELECT * FROM civicrm_contact_relationships " . $order . $limit;

$relationship = new CRM_Contact_DAO_Relationship();

Expand All @@ -1288,10 +1291,15 @@ public static function getRelationship(
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");
}

$mask = NULL;
if ($status != self::INACTIVE) {
if ($links) {
Expand Down Expand Up @@ -1437,6 +1445,7 @@ public static function getRelationship(
}

$relationship->free();
CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS civicrm_contact_relationships");
return $values;
}
}
Expand Down Expand Up @@ -2067,20 +2076,13 @@ public static function getContactRelationshipSelector(&$params) {
$params['rp'], 0, 0,
$links, $mask,
$permissionedContacts,
$params
$params, TRUE
);

$contactRelationships = array();
$params['total'] = 0;
$params['total'] = $relationships['total_relationships'];
unset($relationships['total_relationships']);
if (!empty($relationships)) {
// FIXME: we cannot directly determine total permissioned relationship, hence re-fire query
$permissionedRelationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'],
$relationshipStatus,
0, 0, 0,
NULL, NULL,
$permissionedContacts
);
$params['total'] = count($permissionedRelationships);

// format params
foreach ($relationships as $relationshipId => $values) {
Expand Down