diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index b7d23d1eb9..3b245ad923 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -216,8 +216,8 @@ abstract class Mapper implements IMapper if (empty($aggregation)) { return $this->composeSelectQuery($this->toDb($entity->getEntityName()), $selectPart, $joinsPart, $wherePart, $orderPart, $offset, $limit, $distinct); - } else { - return $this->composeSelectQuery($this->toDb($entity->getEntityName()), $selectPart, $joinsPart, $wherePart, null, null, null, $distinct); + } else { + return $this->composeSelectQuery($this->toDb($entity->getEntityName()), $selectPart, $joinsPart, $wherePart, null, null, null, false, $aggregation = true); } } @@ -1054,6 +1054,52 @@ abstract class Mapper implements IMapper $relTable = $this->toDb($relOpt['relationName']); $distantTable = $this->toDb($relOpt['entity']); + /*$join = + "{$pre}JOIN (" + . "SELECT DISTINCT {$distantTable}.*, {$relTable}." . $this->toDb($nearKey) . ""; + $join .= " FROM `{$distantTable}`"; + + $join .= " JOIN `{$relTable}` ON {$distantTable}." . $this->toDb($foreignKey) . " = {$relTable}." . $this->toDb($distantKey); + + $join .= " WHERE {$distantTable}.deleted = " . $this->pdo->quote(0); + + if (!empty($relOpt['conditions']) && is_array($relOpt['conditions'])) { + $conditions = array_merge($conditions, $relOpt['conditions']); + } + foreach ($conditions as $f => $v) { + $join .= " AND {$relTable}." . $this->toDb($f) . " = " . $this->pdo->quote($v); + } + + $join .= ") `{$distantTable}` ON {$distantTable}." . $this->toDb($nearKey) . " = {$this->toDb($entity->getEntityName())}." . $this->toDb($key); + + return $join;*/ + + + + + /*$join = + "{$pre}JOIN `{$relTable}` ON {$this->toDb($entity->getEntityName())}." . $this->toDb($key) . " = {$relTable}." . $this->toDb($nearKey) + . " AND " + . "{$relTable}.deleted = " . $this->pdo->quote(0); + + if (!empty($relOpt['conditions']) && is_array($relOpt['conditions'])) { + $conditions = array_merge($conditions, $relOpt['conditions']); + } + foreach ($conditions as $f => $v) { + $join .= " AND {$relTable}." . $this->toDb($f) . " = " . $this->pdo->quote($v); + } + + $join .= " {$pre}JOIN (SELECT DISTINCT * FROM `{$distantTable}` " + . " WHERE " + . "{$distantTable}.deleted = " . $this->pdo->quote(0) . ") AS`{$distantTable}`" + . " ON {$distantTable}." . $this->toDb($foreignKey) . " = {$relTable}." . $this->toDb($distantKey); + + return $join;*/ + + + + + $join = "{$pre}JOIN `{$relTable}` ON {$this->toDb($entity->getEntityName())}." . $this->toDb($key) . " = {$relTable}." . $this->toDb($nearKey) . " AND " diff --git a/application/Espo/ORM/DB/MysqlMapper.php b/application/Espo/ORM/DB/MysqlMapper.php index 74c8a21042..ecbd69396e 100644 --- a/application/Espo/ORM/DB/MysqlMapper.php +++ b/application/Espo/ORM/DB/MysqlMapper.php @@ -33,13 +33,13 @@ use PDO; */ class MysqlMapper extends Mapper { - protected function composeSelectQuery($table, $select, $joins = '', $where = '', $order = '', $offset = null, $limit = null, $distinct = null) + protected function composeSelectQuery($table, $select, $joins = '', $where = '', $order = '', $offset = null, $limit = null, $distinct = null, $aggregation = false) { $sql = "SELECT"; - if (!empty($distinct)) { + /*if (!empty($distinct)) { $sql .= " DISTINCT"; - } + }*/ $sql .= " {$select} FROM `{$table}`"; @@ -51,6 +51,10 @@ class MysqlMapper extends Mapper $sql .= " WHERE {$where}"; } + if (!empty($distinct)) { + $sql .= " GROUP BY `{$table}`.id"; + } + if (!empty($order)) { $sql .= " {$order}"; }