This commit is contained in:
Yuri Kuznetsov
2025-06-08 19:35:37 +03:00
parent 9982ed0bef
commit f2c4e0931d
3 changed files with 39 additions and 24 deletions
@@ -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.
*
@@ -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()
)
);
}
@@ -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