Skip to content

EspoCRM – Open Source CRM Application

ref

Yuri Kuznetsovf2c4e0931dc9
diff --git a/application/Espo/Core/Select/Helpers/RelationQueryHelper.php b/application/Espo/Core/Select/Helpers/RelationQueryHelper.php
index 870cec3fc7..83af71783f 100644
--- a/application/Espo/Core/Select/Helpers/RelationQueryHelper.php
+++ b/application/Espo/Core/Select/Helpers/RelationQueryHelper.php
@@ -39,8 +39,11 @@ use Espo\ORM\Query\Part\Expression;
use Espo\ORM\Query\Part\Expression as Expr;
use Espo\ORM\Query\Part\WhereClause;
use Espo\ORM\Query\Part\WhereItem;
+use Espo\ORM\Query\SelectBuilder;
use Espo\ORM\Query\SelectBuilder as QueryBuilder;
use Espo\ORM\Type\RelationType;
+use LogicException;
+use RuntimeException;
/**
* @since 9.0.0
@@ -108,6 +111,32 @@ class RelationQueryHelper
);
}
+ /**
+ * @param string|string[] $id
+ *
+ * @since 9.1.6
+ */
+ public function prepareLinkWhereMany(string $entityType, string $link, string|array $id): WhereItem
+ {
+ $defs = $this->defs
+ ->getEntity($entityType)
+ ->getRelation($link);
+
+ if (!in_array($defs->getType(), [RelationType::HAS_MANY, RelationType::MANY_MANY])) {
+ throw new LogicException("Only many-many and has-many allowed.");
+ }
+
+ $builder = SelectBuilder::create()->from($entityType);
+
+ $whereItem = $this->prepareLinkWhere($defs, $entityType, $id, $builder);
+
+ if (!$whereItem) {
+ throw new RuntimeException("Not supported relationship.");
+ }
+
+ return $whereItem;
+ }
+
/**
* @internal Signature can be changed in future.
*
diff --git a/application/Espo/Modules/Crm/Tools/Activities/Service.php b/application/Espo/Modules/Crm/Tools/Activities/Service.php
index dacb796c0c..40eaf19738 100644
--- a/application/Espo/Modules/Crm/Tools/Activities/Service.php
+++ b/application/Espo/Modules/Crm/Tools/Activities/Service.php
@@ -156,18 +156,12 @@ class Service
);
if ($entity->isPortal() && $entity->getContactId()) {
- $contactsRelation = $this->entityManager
- ->getDefs()
- ->getEntity(Meeting::ENTITY_TYPE)
- ->getRelation('contacts');
-
$orBuilder->add(
- $this->relationQueryHelper->prepareLinkWhere(
- $contactsRelation,
+ $this->relationQueryHelper->prepareLinkWhereMany(
Meeting::ENTITY_TYPE,
- $entity->getContactId(),
- $builder
- ) ?? throw new RuntimeException()
+ 'contacts',
+ $entity->getContactId()
+ )
);
}
@@ -235,18 +229,12 @@ class Service
);
if ($entity->isPortal() && $entity->getContactId()) {
- $contactsRelation = $this->entityManager
- ->getDefs()
- ->getEntity(Meeting::ENTITY_TYPE)
- ->getRelation('contacts');
-
$orBuilder->add(
- $this->relationQueryHelper->prepareLinkWhere(
- $contactsRelation,
+ $this->relationQueryHelper->prepareLinkWhereMany(
Call::ENTITY_TYPE,
- $entity->getContactId(),
- $builder
- ) ?? throw new RuntimeException()
+ 'contacts',
+ $entity->getContactId()
+ )
);
}
diff --git a/application/Espo/Modules/Crm/Tools/Calendar/Service.php b/application/Espo/Modules/Crm/Tools/Calendar/Service.php
index d81a60588f..f897595219 100644
--- a/application/Espo/Modules/Crm/Tools/Calendar/Service.php
+++ b/application/Espo/Modules/Crm/Tools/Calendar/Service.php
@@ -329,11 +329,9 @@ class Service
$usersRelation->getType() === RelationType::MANY_MANY &&
$usersRelation->getForeignEntityType() === User::ENTITY_TYPE
) {
- $whereItem = $this->relationQueryHelper->prepareLinkWhere($usersRelation, $scope, $userId, $queryBuilder);
+ $whereItem = $this->relationQueryHelper->prepareLinkWhereMany($scope, $usersLink, $userId);
- if ($whereItem) {
- $orBuilder->add($whereItem);
- }
+ $orBuilder->add($whereItem);
}
$queryBuilder