diff --git a/application/Espo/Core/FieldProcessing/EmailAddress/LoadProcessor.php b/application/Espo/Core/FieldProcessing/EmailAddress/LoadProcessor.php new file mode 100644 index 0000000000..5418be5c6a --- /dev/null +++ b/application/Espo/Core/FieldProcessing/EmailAddress/LoadProcessor.php @@ -0,0 +1,71 @@ +ormDefs = $ormDefs; + $this->entityManager = $entityManager; + } + + public function process(Entity $entity): void + { + $entityDefs = $this->ormDefs->getEntity($entity->getEntityType()); + + if (!$entityDefs->hasField('emailAddress')) { + return; + } + + if ($entityDefs->getField('emailAddress')->getType() !== 'email') { + return; + } + + $emailAddressData = $this->entityManager + ->getRepository('EmailAddress') + ->getEmailAddressData($entity); + + $entity->set('emailAddressData', $emailAddressData); + $entity->setFetched('emailAddressData', $emailAddressData); + } +} diff --git a/application/Espo/Core/FieldProcessing/GeneralLoadProcessor.php b/application/Espo/Core/FieldProcessing/GeneralLoadProcessor.php index 9dc543f09f..caedc01b6e 100644 --- a/application/Espo/Core/FieldProcessing/GeneralLoadProcessor.php +++ b/application/Espo/Core/FieldProcessing/GeneralLoadProcessor.php @@ -45,6 +45,8 @@ class GeneralLoadProcessor private $metadata; + private $processorListMapCache = []; + public function __construct(InjectableFactory $injectableFactory, Metadata $metadata) { $this->injectableFactory = $injectableFactory; @@ -53,7 +55,7 @@ class GeneralLoadProcessor public function process(Entity $entity): void { - foreach ($this->createProcessorList($entity->getEntityType()) as $processor) { + foreach ($this->getProcessorList($entity->getEntityType()) as $processor) { $processor->process($entity); } } @@ -61,14 +63,20 @@ class GeneralLoadProcessor /** * @return LoadProcessor[] */ - private function createProcessorList(string $entityType): array + private function getProcessorList(string $entityType): array { + if (array_key_exists($entityType, $this->processorListMapCache)) { + return $this->processorListMapCache[$entityType]; + } + $list = []; foreach ($this->getProcessorClassNameList($entityType) as $className) { $list[] = $this->createProcessor($className); } + $this->processorListMapCache[$entityType] = $list; + return $list; } diff --git a/application/Espo/Core/FieldProcessing/Link/LoadProcessor.php b/application/Espo/Core/FieldProcessing/Link/HasOneLoadProcessor.php similarity index 95% rename from application/Espo/Core/FieldProcessing/Link/LoadProcessor.php rename to application/Espo/Core/FieldProcessing/Link/HasOneLoadProcessor.php index 6fa6d24970..6a9dbb4991 100644 --- a/application/Espo/Core/FieldProcessing/Link/LoadProcessor.php +++ b/application/Espo/Core/FieldProcessing/Link/HasOneLoadProcessor.php @@ -36,7 +36,7 @@ use Espo\Core\{ use Espo\ORM\Defs\Defs as OrmDefs; -class LoadProcessor implements LoadProcessorInterface +class HasOneLoadProcessor implements LoadProcessorInterface { private $ormDefs; @@ -50,6 +50,10 @@ class LoadProcessor implements LoadProcessorInterface public function process(Entity $entity): void { foreach ($this->getFieldList($entity->getEntityType()) as $field) { + if ($entity->get($field . 'Name')) { + continue; + } + $entity->loadLinkField($field); } } diff --git a/application/Espo/Core/FieldProcessing/Link/NotJoinedLoadProcessor.php b/application/Espo/Core/FieldProcessing/Link/NotJoinedLoadProcessor.php new file mode 100644 index 0000000000..abcd2bdd07 --- /dev/null +++ b/application/Espo/Core/FieldProcessing/Link/NotJoinedLoadProcessor.php @@ -0,0 +1,147 @@ +ormDefs = $ormDefs; + $this->entityManager = $entityManager; + } + + public function process(Entity $entity): void + { + foreach ($this->getFieldList($entity->getEntityType()) as $field) { + $this->processItem($entity, $field); + } + } + + private function processItem(Entity $entity, string $field): void + { + $nameAttribute = $field . 'Name'; + $idAttribute = $field . 'Id'; + + $id = $entity->get($idAttribute); + + if (!$id) { + $entity->set($nameAttribute, null); + + return; + } + + if ($entity->get($nameAttribute)) { + return; + } + + $foreignEntityType = $this->ormDefs + ->getEntity($entity->getEntityType()) + ->getRelation($field) + ->getForeignEntityType(); + + $foreignEntity = $this->entityManager + ->getRDBRepository($foreignEntityType) + ->select(['id', 'name']) + ->where(['id' => $id]) + ->findOne(); + + if (!$foreignEntity) { + $entity->set($nameAttribute, null); + + return; + } + + $entity->set($nameAttribute, $foreignEntity->get('name')); + } + + /** + * @return string[] + */ + private function getFieldList(string $entityType): array + { + if (array_key_exists($entityType, $this->fieldListCacheMap)) { + return $this->fieldListCacheMap[$entityType]; + } + + $list = []; + + $entityDefs = $this->ormDefs->getEntity($entityType); + + foreach ($entityDefs->getRelationList() as $relationDefs) { + if ($relationDefs->getType() !== Entity::BELONGS_TO) { + continue; + } + + if (!$relationDefs->getParam('noJoin')) { + continue; + } + + if (!$relationDefs->hasForeignEntityType()) { + continue; + } + + $foreignEntityType = $relationDefs->getForeignEntityType(); + + if (!$this->entityManager->hasRepository($foreignEntityType)) { + continue; + } + + $name = $relationDefs->getName(); + + if (!$entityDefs->hasAttribute($name . 'Id')) { + continue; + } + + if (!$entityDefs->hasAttribute($name . 'Name') ) { + continue; + } + + $list[] = $name; + } + + $this->fieldListCacheMap[$entityType] = $list; + + return $list; + } +} diff --git a/application/Espo/Core/FieldProcessing/PhoneNumber/LoadProcessor.php b/application/Espo/Core/FieldProcessing/PhoneNumber/LoadProcessor.php new file mode 100644 index 0000000000..da7bdc84e2 --- /dev/null +++ b/application/Espo/Core/FieldProcessing/PhoneNumber/LoadProcessor.php @@ -0,0 +1,71 @@ +ormDefs = $ormDefs; + $this->entityManager = $entityManager; + } + + public function process(Entity $entity): void + { + $entityDefs = $this->ormDefs->getEntity($entity->getEntityType()); + + if (!$entityDefs->hasField('phoneNumber')) { + return; + } + + if ($entityDefs->getField('phoneNumber')->getType() !== 'phone') { + return; + } + + $phoneNumberData = $this->entityManager + ->getRepository('PhoneNumber') + ->getPhoneNumberData($entity); + + $entity->set('phoneNumberData', $phoneNumberData); + $entity->setFetched('phoneNumberData', $phoneNumberData); + } +} diff --git a/application/Espo/Resources/metadata/app/fieldProcessing.json b/application/Espo/Resources/metadata/app/fieldProcessing.json index 539896d10d..3364c449fe 100644 --- a/application/Espo/Resources/metadata/app/fieldProcessing.json +++ b/application/Espo/Resources/metadata/app/fieldProcessing.json @@ -1,8 +1,11 @@ { "loadProcessorClassNameList": [ - "Espo\\Core\\FieldProcessing\\Link\\LoadProcessor", + "Espo\\Core\\FieldProcessing\\Link\\HasOneLoadProcessor", + "Espo\\Core\\FieldProcessing\\Link\\NotJoinedLoadProcessor", "Espo\\Core\\FieldProcessing\\LinkMultiple\\LoadProcessor", "Espo\\Core\\FieldProcessing\\LinkParent\\LoadProcessor", + "Espo\\Core\\FieldProcessing\\EmailAddress\\LoadProcessor", + "Espo\\Core\\FieldProcessing\\PhoneNumber\\LoadProcessor", "Espo\\Core\\FieldProcessing\\Misc\\StreamLoadProcessor" ] } diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 2ae8c0b0dd..f6dcfb9e57 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -567,98 +567,6 @@ class Record implements Crud, } } - protected function loadNotJoinedLinkFields(Entity $entity) - { - $linkDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.links', []); - - foreach ($linkDefs as $link => $defs) { - $type = $defs['type'] ?? null; - - if ($type !== 'belongsTo') { - continue; - } - - if (empty($defs['noJoin']) || empty($defs['entity'])) { - continue; - } - - $nameAttribute = $link . 'Name'; - $idAttribute = $link . 'Id'; - - if (!$entity->hasAttribute($nameAttribute) || $entity->hasAttribute($idAttribute)) { - continue; - } - - $id = $entity->get($idAttribute); - - if (!$id) { - $entity->set($nameAttribute, null); - - continue; - } - - $scope = $defs['entity']; - - if (!$this->getEntityManager()->hasRepository($scope)) { - continue; - } - - $foreignEntity = $this->getEntityManager() - ->getRepository($scope) - ->select(['id', 'name']) - ->where(['id' => $id]) - ->findOne(); - - if ($foreignEntity) { - $entity->set($nameAttribute, $foreignEntity->get('name')); - - continue; - } - - $entity->set($nameAttribute, null); - } - } - - protected function loadEmptyNameLinkFields(Entity $entity) - { - $linkDefs = $this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'links'], []); - - foreach ($linkDefs as $link => $defs) { - if (!isset($defs['type'])) { - continue; - } - - if ($defs['type'] !== 'belongsTo') { - continue; - } - - $nameAttribute = $link . 'Name'; - $idAttribute = $link . 'Id'; - - if ($entity->get($idAttribute) && !$entity->get($nameAttribute)) { - $id = $entity->get($idAttribute); - - if (empty($defs['entity'])) { - continue; - } - - $scope = $defs['entity']; - - if ($this->getEntityManager()->hasRepository($scope)) { - $foreignEntity = $this->getEntityManager() - ->getRepository($scope) - ->select(['id', 'name']) - ->where(['id' => $id]) - ->findOne(); - - if ($foreignEntity) { - $entity->set($nameAttribute, $foreignEntity->get('name')); - } - } - } - } - } - private function createLoadProcessor(): GeneralLoadProcessor { return $this->injectableFactory->create(GeneralLoadProcessor::class); @@ -669,13 +577,6 @@ class Record implements Crud, $loadProcessor = $this->createLoadProcessor(); $loadProcessor->process($entity); - - //$this->loadParentNameFields($entity); - - $this->loadEmailAddressField($entity); - $this->loadPhoneNumberField($entity); - $this->loadNotJoinedLinkFields($entity); - $this->loadEmptyNameLinkFields($entity); } public function loadAdditionalFieldsForList(Entity $entity) @@ -687,38 +588,6 @@ class Record implements Crud, { } - protected function loadEmailAddressField(Entity $entity) - { - $fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.fields', []); - - if (!empty($fieldDefs['emailAddress']) && $fieldDefs['emailAddress']['type'] == 'email') { - $dataAttributeName = 'emailAddressData'; - - $emailAddressData = $this->getEntityManager() - ->getRepository('EmailAddress') - ->getEmailAddressData($entity); - - $entity->set($dataAttributeName, $emailAddressData); - $entity->setFetched($dataAttributeName, $emailAddressData); - } - } - - protected function loadPhoneNumberField(Entity $entity) - { - $fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.fields', []); - - if (!empty($fieldDefs['phoneNumber']) && $fieldDefs['phoneNumber']['type'] == 'phone') { - $dataAttributeName = 'phoneNumberData'; - - $phoneNumberData = $this->getEntityManager() - ->getRepository('PhoneNumber') - ->getPhoneNumberData($entity); - - $entity->set($dataAttributeName, $phoneNumberData); - $entity->setFetched($dataAttributeName, $phoneNumberData); - } - } - /** * @deprecated */