From 49aea5aad13f8edeea14fc73fdea0aee7e32d887 Mon Sep 17 00:00:00 2001 From: yuri Date: Tue, 30 Oct 2018 12:45:03 +0200 Subject: [PATCH] load empty link name fields --- application/Espo/Services/Record.php | 33 +++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index bcacb4cba0..f2b83b4616 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -336,9 +336,8 @@ class Record extends \Espo\Core\Services\Base if ($entity->hasAttribute($nameAttribute) && $entity->hasAttribute($idAttribute)) { $id = $entity->get($idAttribute); } - - $scope = $defs['entity']; - if (!empty($scope)) { + if (!empty($defs['entity'])) { + $scope = $defs['entity']; if ($this->getEntityManager()->hasRepository($scope)) { $foreignEntity = $this->getEntityManager()->getRepository($scope) ->select(['id', 'name']) @@ -354,6 +353,33 @@ class Record extends \Espo\Core\Services\Base } } + 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')); + } + } + } + } + } + public function loadAdditionalFields(Entity $entity) { $this->loadLinkFields($entity); @@ -364,6 +390,7 @@ class Record extends \Espo\Core\Services\Base $this->loadEmailAddressField($entity); $this->loadPhoneNumberField($entity); $this->loadNotJoinedLinkFields($entity); + $this->loadEmptyNameLinkFields($entity); } public function loadAdditionalFieldsForList(Entity $entity)