From 4cc33fc06e87ee1e9cd0ec42d4f9563a5f6c46e4 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 11 Feb 2019 13:23:18 +0200 Subject: [PATCH] orm selectForeign --- .../Utils/Database/Orm/Fields/Currency.php | 16 +++++- application/Espo/ORM/DB/Query/Base.php | 53 ++++++++++++++----- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php b/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php index 0afaddcab0..3f8d2be9cb 100644 --- a/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php +++ b/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php @@ -60,6 +60,8 @@ class Currency extends Base ] ]; + $foreignAlias = "{$alias}{$entityType}Foreign"; + $params = $this->getFieldParams($fieldName); if (!empty($params['notStorable'])) { $defs[$entityType]['fields'][$fieldName]['notStorable'] = true; @@ -70,6 +72,18 @@ class Currency extends Base 'sql' => $part . " * {$alias}.rate", 'leftJoins' => $leftJoins, ], + 'selectForeign' => [ + 'sql' => "{alias}.{$currencyColumnName} * {$foreignAlias}.rate", + 'leftJoins' => [ + [ + 'Currency', + $foreignAlias, + [ + $foreignAlias . '.id:' => "{alias}.{$fieldName}Currency" + ] + ] + ], + ], 'where' => [ "=" => ['sql' => $part . " * {$alias}.rate = {value}", 'leftJoins' => $leftJoins], @@ -85,7 +99,7 @@ class Currency extends Base 'orderBy' => [ 'sql' => $converedFieldName . " {direction}", 'leftJoins' => $leftJoins, - ] + ], ]; $defs[$entityType]['fields'][$fieldName]['orderBy'] = [ diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php index 9e2b7a3225..4e605be2f3 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -414,44 +414,55 @@ abstract class Base return $result; } - protected function convertComplexExpression($entity, $field, $distinct = false, &$params = null) + protected function convertComplexExpression($entity, $attribute, $distinct = false, &$params = null) { $function = null; $relName = null; $entityType = $entity->getEntityType(); - if (strpos($field, ':')) { - $dilimeterPosition = strpos($field, ':'); - $function = substr($field, 0, $dilimeterPosition); + if (strpos($attribute, ':')) { + $dilimeterPosition = strpos($attribute, ':'); + $function = substr($attribute, 0, $dilimeterPosition); if (in_array($function, $this->matchFunctionList)) { - return $this->convertMatchExpression($entity, $field); + return $this->convertMatchExpression($entity, $attribute); } - $field = substr($field, $dilimeterPosition + 1); + $attribute = substr($attribute, $dilimeterPosition + 1); } if (!empty($function)) { $function = preg_replace('/[^A-Za-z0-9_]+/', '', $function); } - if (strpos($field, '.')) { - list($relName, $field) = explode('.', $field); + if (strpos($attribute, '.')) { + list($relName, $attribute) = explode('.', $attribute); } if (!empty($relName)) { $relName = preg_replace('/[^A-Za-z0-9_]+/', '', $relName); } - if (!empty($field)) { - $field = preg_replace('/[^A-Za-z0-9_]+/', '', $field); + if (!empty($attribute)) { + $attribute = preg_replace('/[^A-Za-z0-9_]+/', '', $attribute); } - $part = $this->toDb($field); + $part = $this->toDb($attribute); if ($relName) { $part = $relName . '.' . $part; + + $foreignEntityType = $entity->getRelationParam($relName, 'entity'); + if ($foreignEntityType) { + $foreignSeed = $this->getSeed($foreignEntityType); + if ($foreignSeed) { + $selectForeign = $foreignSeed->getAttributeParam($attribute, 'selectForeign'); + if (is_array($selectForeign)) { + $part = $this->getAttributeSql($foreignSeed, $attribute, 'selectForeign', $params, $relName); + } + } + } } else { - if (!empty($entity->fields[$field]['select'])) { - $part = $this->getAttributeSql($entity, $field, 'select', $params); + if (!empty($entity->fields[$attribute]['select'])) { + $part = $this->getAttributeSql($entity, $attribute, 'select', $params); } else { $part = $this->toDb($entityType) . '.' . $part; } @@ -462,7 +473,7 @@ abstract class Base return $part; } - protected function getAttributeSql(IEntity $entity, $attribute, $type, &$params = null) + protected function getAttributeSql(IEntity $entity, $attribute, $type, &$params = null, $alias = null) { $fieldDefs = $entity->fields[$attribute]; @@ -471,6 +482,9 @@ abstract class Base } else { if (!empty($fieldDefs[$type]['sql'])) { $part = $fieldDefs[$type]['sql']; + if ($alias) { + $part = str_replace('{alias}', $alias, $part); + } } else { $part = $this->toDb($entity->getEntityType()) . '.' . $this->toDb($attribute); if ($type === 'orderBy') { @@ -489,6 +503,17 @@ abstract class Base continue 2; } } + if ($alias) { + if (count($j) >= 3) { + $conditions = []; + foreach ($j[2] as $k => $value) { + $value = str_replace('{alias}', $alias, $value); + $conditions[$k] = $value; + } + $j[2] = $conditions; + } + } + $params['leftJoins'][] = $j; } }