From b0438991bf3b1102564a15e19f47be028ab49d33 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 22 Jan 2018 15:52:13 +0200 Subject: [PATCH] entity manager refactoring --- application/Espo/Core/ORM/Entity.php | 18 ++++++++++++++++++ application/Espo/ORM/EntityManager.php | 13 +++++++++---- application/Espo/ORM/Metadata.php | 8 ++++++++ application/Espo/Services/Record.php | 11 ++++------- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/application/Espo/Core/ORM/Entity.php b/application/Espo/Core/ORM/Entity.php index b9d92dacff..f9b289912f 100644 --- a/application/Espo/Core/ORM/Entity.php +++ b/application/Espo/Core/ORM/Entity.php @@ -41,6 +41,24 @@ class Entity extends \Espo\ORM\Entity return $this->hasAttribute($field . 'Id'); } + public function loadParentNameField($field) + { + if (!$this->hasAttribute($field. 'Id') || !$this->hasAttribute($field . 'Type')) return; + + $parentId = $this->get($field . 'Id'); + $parentType = $this->get($field . 'Type'); + + if ($parentId && $parentType) { + if (!$this->entityManager->hasRepository($parentType)) return; + $repository = $this->entityManager->getRepository($parentType); + + $foreignEntity = $repository->select(['id', 'name'])->where(['id' => $parentId])->findOne(); + if ($foreignEntity) { + $this->set($field . 'Name', $foreignEntity->get('name')); + } + } + } + public function loadLinkMultipleField($field, $columns = null) { if (!$this->hasRelation($field) || !$this->hasAttribute($field . 'Ids')) return; diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 16f05afe12..d894f73fef 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -163,14 +163,14 @@ class EntityManager public function saveEntity(Entity $entity, array $options = array()) { - $entityName = $entity->getEntityName(); - return $this->getRepository($entityName)->save($entity, $options); + $entityType = $entity->getEntityType(); + return $this->getRepository($entityType)->save($entity, $options); } public function removeEntity(Entity $entity, array $options = array()) { - $entityName = $entity->getEntityName(); - return $this->getRepository($entityName)->remove($entity, $options); + $entityType = $entity->getEntityType(); + return $this->getRepository($entityType)->remove($entity, $options); } public function getRepository($name) @@ -186,6 +186,11 @@ class EntityManager $this->metadata->setData($data); } + public function hasRepository($name) + { + return $this->getMetadata()->has($name); + } + public function getMetadata() { return $this->metadata; diff --git a/application/Espo/ORM/Metadata.php b/application/Espo/ORM/Metadata.php index a370f1bd68..81d62e8f31 100644 --- a/application/Espo/ORM/Metadata.php +++ b/application/Espo/ORM/Metadata.php @@ -45,4 +45,12 @@ class Metadata } return $this->data[$entityType]; } + + public function has($entityType) + { + if (!array_key_exists($entityType, $this->data)) { + return null; + } + return true; + } } diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index cca2257bae..98d061c92d 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -282,13 +282,10 @@ class Record extends \Espo\Core\Services\Base $fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.fields', array()); foreach ($fieldDefs as $field => $defs) { if (isset($defs['type']) && $defs['type'] == 'linkParent') { - $id = $entity->get($field . 'Id'); - $scope = $entity->get($field . 'Type'); - - if ($scope) { - if ($foreignEntity = $this->getEntityManager()->getEntity($scope, $id)) { - $entity->set($field . 'Name', $foreignEntity->get('name')); - } + $parentId = $entity->get($field . 'Id'); + $parentType = $entity->get($field . 'Type'); + if ($parentId && $parentType) { + $entity->loadParentNameField($field); } } }