diff --git a/application/Espo/Classes/Acl/Email/LinkCheckers/ParentLinkChecker.php b/application/Espo/Classes/Acl/Email/LinkCheckers/ParentLinkChecker.php index 865497eea6..b88d3d257c 100644 --- a/application/Espo/Classes/Acl/Email/LinkCheckers/ParentLinkChecker.php +++ b/application/Espo/Classes/Acl/Email/LinkCheckers/ParentLinkChecker.php @@ -65,12 +65,12 @@ class ParentLinkChecker implements LinkChecker return false; } - $parentLink = $replied->getParent(); + $parent = $replied->getParent(); if ( - !$parentLink || - $parentLink->getId() !== $foreignEntity->getId() || - $parentLink->getEntityType() !== $foreignEntity->getEntityType() + !$parent || + $parent->getId() !== $foreignEntity->getId() || + $parent->getEntityType() !== $foreignEntity->getEntityType() ) { return false; } diff --git a/application/Espo/Classes/AssignmentNotificators/Email.php b/application/Espo/Classes/AssignmentNotificators/Email.php index 347b95f187..a8da9299d8 100644 --- a/application/Espo/Classes/AssignmentNotificators/Email.php +++ b/application/Espo/Classes/AssignmentNotificators/Email.php @@ -183,13 +183,8 @@ class Email implements AssignmentNotificator } } - $parent = $entity->getParent() ? - $this->entityManager->getEntityById($entity->getParent()->getEntityType(), $entity->getParent()->getId()) : - null; - - $account = $entity->getAccount() ? - $this->entityManager->getEntityById(Account::ENTITY_TYPE, $entity->getAccount()->getId()) : - null; + $parent = $entity->getParent(); + $account = $entity->getAccount(); foreach ($userIdList as $userId) { if ($userIdFrom === $userId) { diff --git a/application/Espo/Core/Mail/Account/GroupAccount/Hooks/AfterFetch.php b/application/Espo/Core/Mail/Account/GroupAccount/Hooks/AfterFetch.php index 87ad960588..451c25c122 100644 --- a/application/Espo/Core/Mail/Account/GroupAccount/Hooks/AfterFetch.php +++ b/application/Espo/Core/Mail/Account/GroupAccount/Hooks/AfterFetch.php @@ -132,19 +132,11 @@ class AfterFetch implements AfterFetchInterface private function noteAboutEmail(Email $email): void { - $parentLink = $email->getParent(); - - if (!$parentLink) { + if (!$email->getParent()) { return; } - $parent = $this->entityManager->getEntityById($parentLink->getEntityType(), $parentLink->getId()); - - if (!$parent) { - return; - } - - $this->streamService->noteEmailReceived($parent, $email); + $this->streamService->noteEmailReceived($email->getParent(), $email); } private function autoReply( diff --git a/application/Espo/Core/Mail/Account/PersonalAccount/Hooks/AfterFetch.php b/application/Espo/Core/Mail/Account/PersonalAccount/Hooks/AfterFetch.php index df8e303b3f..2365b05e82 100644 --- a/application/Espo/Core/Mail/Account/PersonalAccount/Hooks/AfterFetch.php +++ b/application/Espo/Core/Mail/Account/PersonalAccount/Hooks/AfterFetch.php @@ -34,12 +34,10 @@ use Espo\Core\Mail\Account\Hook\BeforeFetchResult; use Espo\Core\Mail\Account\Hook\AfterFetch as AfterFetchInterface; use Espo\Tools\Stream\Service as StreamService; use Espo\Entities\Email; -use Espo\ORM\EntityManager; class AfterFetch implements AfterFetchInterface { public function __construct( - private EntityManager $entityManager, private StreamService $streamService ) {} @@ -52,18 +50,10 @@ class AfterFetch implements AfterFetchInterface private function noteAboutEmail(Email $email): void { - $parentLink = $email->getParent(); - - if (!$parentLink) { + if (!$email->getParent()) { return; } - $parent = $this->entityManager->getEntityById($parentLink->getEntityType(), $parentLink->getId()); - - if (!$parent) { - return; - } - - $this->streamService->noteEmailReceived($parent, $email); + $this->streamService->noteEmailReceived($email->getParent(), $email); } } diff --git a/application/Espo/Core/Mail/Importer/DefaultImporter.php b/application/Espo/Core/Mail/Importer/DefaultImporter.php index f49f946edd..de65284db1 100644 --- a/application/Espo/Core/Mail/Importer/DefaultImporter.php +++ b/application/Espo/Core/Mail/Importer/DefaultImporter.php @@ -250,15 +250,13 @@ class DefaultImporter implements Importer private function processEmailWithParent(Email $email): void { - $parentLink = $email->getParent(); + $parentType = $email->get(Field::PARENT . 'Type'); + $parentId = $email->get(Field::PARENT . 'Id'); - if (!$parentLink) { + if (!$parentId || !$parentType) { return; } - $parentType = $parentLink->getEntityType(); - $parentId = $parentLink->getId(); - $emailKeepParentTeamsEntityList = $this->config->get('emailKeepParentTeamsEntityList') ?? []; if ( @@ -268,7 +266,7 @@ class DefaultImporter implements Importer return; } - $parent = $this->entityManager->getEntityById($parentType, $parentId); + $parent = $email->getParent(); if (!$parent) { return; diff --git a/application/Espo/Core/Mail/Importer/DefaultParentFinder.php b/application/Espo/Core/Mail/Importer/DefaultParentFinder.php index 57b8295b1b..5083048d69 100644 --- a/application/Espo/Core/Mail/Importer/DefaultParentFinder.php +++ b/application/Espo/Core/Mail/Importer/DefaultParentFinder.php @@ -201,21 +201,7 @@ class DefaultParentFinder implements ParentFinder ->getRDBRepositoryByClass(Email::class) ->getById($repliedLink->getId()); - if (!$repliedEmail) { - return null; - } - - $parentLink = $repliedEmail->getParent(); - - if (!$parentLink) { - return null; - } - - if (!$this->entityManager->hasRepository($parentLink->getEntityType())) { - return null; - } - - return $this->entityManager->getEntityById($parentLink->getEntityType(), $parentLink->getId()); + return $repliedEmail?->getParent(); } private function getByReferences(Message $message): ?Entity diff --git a/application/Espo/Entities/Email.php b/application/Espo/Entities/Email.php index 489d00aa01..39a0ad8fc3 100644 --- a/application/Espo/Entities/Email.php +++ b/application/Espo/Entities/Email.php @@ -647,10 +647,10 @@ class Email extends Entity return $this->get('parentId'); } - public function getParent(): ?LinkParent + public function getParent(): ?OrmEntity { - /** @var ?LinkParent */ - return $this->getValueObject(Field::PARENT); + /** @var ?OrmEntity */ + return $this->relations->getOne(Field::PARENT); } public function setAccount(Link|Account|null $account): self @@ -668,10 +668,10 @@ class Email extends Entity return $this->get('status'); } - public function getAccount(): ?Link + public function getAccount(): ?Account { - /** @var ?Link */ - return $this->getValueObject('account'); + /** @var ?Account */ + return $this->relations->getOne('account'); } public function getTeams(): LinkMultiple diff --git a/application/Espo/Entities/Note.php b/application/Espo/Entities/Note.php index ca53677c45..ad4057a830 100644 --- a/application/Espo/Entities/Note.php +++ b/application/Espo/Entities/Note.php @@ -226,44 +226,28 @@ class Note extends Entity return $this->set('type', $type); } - public function setParent(LinkParent|OrmEntity $parent): self + public function setParent(LinkParent|OrmEntity|null $parent): self { - if ($parent instanceof LinkParent) { - $this->setValueObject(Field::PARENT, $parent); - - return $this; - } - - $this->relations->set(Field::PARENT, $parent); - - return $this; + return $this->setRelatedLinkOrEntity(Field::PARENT, $parent); } - public function setRelated(LinkParent|OrmEntity $related): self + public function setRelated(LinkParent|OrmEntity|null $related): self { - if ($related instanceof LinkParent) { - $this->setValueObject('related', $related); - - return $this; - } - - $this->relations->set('related', $related); - - return $this; + return $this->setRelatedLinkOrEntity('related', $related); } - public function setSuperParent(LinkParent|OrmEntity $superParent): self + public function setSuperParent(LinkParent|OrmEntity|null $superParent): self { if ($superParent instanceof LinkParent) { - $this->set('superParentId', $superParent->getId()); - $this->set('superParentType', $superParent->getEntityType()); + $this->setMultiple([ + 'superParentId' => $superParent->getId(), + 'superParentType' => $superParent->getEntityType(), + ]); return $this; } - $this->relations->set('superParent', $superParent); - - return $this; + return $this->setRelatedLinkOrEntity('superParent', $superParent); } public function setPost(?string $post): self diff --git a/application/Espo/Repositories/Email.php b/application/Espo/Repositories/Email.php index 4442962b23..c37dc33f14 100644 --- a/application/Espo/Repositories/Email.php +++ b/application/Espo/Repositories/Email.php @@ -407,12 +407,7 @@ class Email extends Database implements $entity->setAccount(null); } - if (!$entity->getParent()) { - return; - } - - $parent = $this->entityManager - ->getEntityById($entity->getParent()->getEntityType(), $entity->getParent()->getId()); + $parent = $entity->getParent(); if (!$parent) { return; diff --git a/application/Espo/Tools/Stream/Service.php b/application/Espo/Tools/Stream/Service.php index bcdf2b3efe..8ee07db37f 100644 --- a/application/Espo/Tools/Stream/Service.php +++ b/application/Espo/Tools/Stream/Service.php @@ -493,7 +493,7 @@ class Service $this->processNoteTeamsUsers($note, $email); if ($email->getAccount()) { - $note->setSuperParent(LinkParent::create(Account::ENTITY_TYPE, $email->getAccount()->getId())); + $note->setSuperParent($email->getAccount()); } $withContent = in_array($entityType, $this->config->get('streamEmailWithContentEntityTypeList', [])); @@ -553,7 +553,7 @@ class Service $this->processNoteTeamsUsers($note, $email); if ($email->getAccount()) { - $note->setSuperParent(LinkParent::create(Account::ENTITY_TYPE, $email->getAccount()->getId())); + $note->setSuperParent($email->getAccount()); } $withContent = in_array($entityType, $this->config->get('streamEmailWithContentEntityTypeList', []));