From cc357e0060f72ab5b9ba4569d30b778c86c939e4 Mon Sep 17 00:00:00 2001
From: Alan Lord <alanslists@gmail.com>
Date: Sun, 2 Aug 2020 13:07:58 +0100
Subject: [PATCH] Fix for performance and invalid crmid retrieval from
 crmentityrel. Refs #1016

---
 modules/Vtiger/models/Module.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/modules/Vtiger/models/Module.php b/modules/Vtiger/models/Module.php
index 6a94a0e66..2e7d26d4c 100644
--- a/modules/Vtiger/models/Module.php
+++ b/modules/Vtiger/models/Module.php
@@ -1675,7 +1675,8 @@ class Vtiger_Module_Model extends Vtiger_Module {
                     if($tablename == 'vtiger_crmentityrel'){    
                         $sql .= ' LEFT JOIN vtiger_activity ON vtiger_activity.activityid = vtiger_crmentity.crmid ';
                         $sql .= " INNER JOIN $tablename ON ($tablename.relcrmid = vtiger_crmentity.crmid OR $tablename.crmid = vtiger_crmentity.crmid)
-                            WHERE ($tablename.crmid IN (".  generateQuestionMarks($recordIds).")) OR ($tablename.relcrmid IN (".  generateQuestionMarks($recordIds)."))";
+                                  WHERE ($tablename.crmid IN (".  generateQuestionMarks($recordIds).") AND ($tablename.relmodule = '".$module."')) 
+                                    OR ($tablename.relcrmid IN (".  generateQuestionMarks($recordIds).") AND ($tablename.module = '".$module."'))";
 							foreach ($recordIds as $key => $recordId) {
 							array_push($params, $recordId);
 							}
-- 
GitLab