entity manager refactoring
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user