From 717f5f6e2e7efc8f73973a0103d3b07d1dd2efb3 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 13 Feb 2021 13:45:19 +0200 Subject: [PATCH] refactoring --- .../ORM/QueryComposer/BaseQueryComposer.php | 29 ++++++++++--------- application/Espo/Services/Record.php | 6 +++- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/application/Espo/ORM/QueryComposer/BaseQueryComposer.php b/application/Espo/ORM/QueryComposer/BaseQueryComposer.php index b5c46fe43e..c2038cd9e9 100644 --- a/application/Espo/ORM/QueryComposer/BaseQueryComposer.php +++ b/application/Espo/ORM/QueryComposer/BaseQueryComposer.php @@ -1447,7 +1447,7 @@ abstract class BaseQueryComposer implements QueryComposer $value = str_replace('{alias}', $alias, $value); $left = $k; $left = str_replace('{alias}', $alias, $left); - + $conditions[$left] = $value; } @@ -1803,19 +1803,18 @@ abstract class BaseQueryComposer implements QueryComposer } protected function getBelongsToJoinItemPart( - Entity $entity, string $relationName, $r = null, ?string $alias = null, ?array $params = null + Entity $entity, string $relationName, ?string $alias = null, ?array $params = null ) : ?string { - if (empty($r)) { - $r = $entity->relations[$relationName]; - } $keySet = $this->helper->getRelationKeys($entity, $relationName); + $key = $keySet['key']; $foreignKey = $keySet['foreignKey']; if (!$alias) { $alias = $this->getAlias($entity, $relationName); - } else { + } + else { $alias = $this->sanitizeSelectAlias($alias); } @@ -1823,7 +1822,10 @@ abstract class BaseQueryComposer implements QueryComposer return null; } - $table = $this->toDb($r['entity']); + $foreignEntityType = $entity->getRelationParam($relationName, 'entity'); + + $table = $this->toDb($foreignEntityType); + $fromAlias = $this->getFromAlias($params, $entity->getEntityType()); return @@ -1880,11 +1882,11 @@ abstract class BaseQueryComposer implements QueryComposer } } - foreach ($entity->getRelations() as $relationName => $r) { - $type = $r['type'] ?? null; + foreach ($entity->getRelationList() as $relationName) { + $type = $entity->getRelationType($relationName); - if ($type == Entity::BELONGS_TO || $type == Entity::HAS_ONE) { - if (!empty($r['noJoin'])) { + if ($type === Entity::BELONGS_TO || $type === Entity::HAS_ONE) { + if ($entity->getRelationParam($relationName, 'noJoin')) { continue; } @@ -1907,7 +1909,7 @@ abstract class BaseQueryComposer implements QueryComposer } if ($type == Entity::BELONGS_TO) { - $join = $this->getBelongsToJoinItemPart($entity, $relationName, $r, null, $params); + $join = $this->getBelongsToJoinItemPart($entity, $relationName, null, $params); if (!$join) { continue; @@ -1917,6 +1919,7 @@ abstract class BaseQueryComposer implements QueryComposer } else if ($type == Entity::HAS_ONE) { $join = $this->getJoinItemPart($entity, $relationName, true, [], null, $params); + $joinsArr[] = $join; } } @@ -2982,7 +2985,7 @@ abstract class BaseQueryComposer implements QueryComposer return $sql; case Entity::BELONGS_TO: - $sql = $prefix . $this->getBelongsToJoinItemPart($entity, $relationName, null, $alias, $params); + $sql = $prefix . $this->getBelongsToJoinItemPart($entity, $relationName, $alias, $params); return $sql; } diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 0a9ca9fb2f..20fea9841a 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -1569,7 +1569,11 @@ class Record implements Crud, return $this->$methodName($id, $params); } - $foreignEntityType = $entity->relations[$link]['entity']; + $foreignEntityType = $this->entityManager + ->getDefs() + ->getEntity($this->entityType) + ->getRelation($link) + ->getForeignEntityType(); $linkParams = $this->linkParams[$link] ?? [];