From ec4afe0e11d3c55b332d5bd99fa1d31c6d06de9d Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 6 Mar 2020 12:48:45 +0200 Subject: [PATCH] foreign concat name null if empty --- application/Espo/ORM/DB/Query/Base.php | 4 +++- tests/unit/Espo/ORM/DB/MapperTest.php | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php index 679b465e0f..9259e152c8 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -1401,8 +1401,10 @@ abstract class Base $fieldPath = 'TRIM(CONCAT(' . implode(', ', $foreign). '))'; if ($wsCount > 1) { - $fieldPath = "REPLACE({$fieldPath}, ' ', ' ')"; + $fieldPath = "REPLACE({$fieldPath}, ' ', ' ')"; } + + $fieldPath = "NULLIF({$fieldPath}, '')"; } else { $expression = $this->getAlias($entity, $relationName) . '.' . $foreign; $fieldPath = $this->convertComplexExpression($entity, $expression, false, $params); diff --git a/tests/unit/Espo/ORM/DB/MapperTest.php b/tests/unit/Espo/ORM/DB/MapperTest.php index 02e7be8d6d..9deef2a34c 100644 --- a/tests/unit/Espo/ORM/DB/MapperTest.php +++ b/tests/unit/Espo/ORM/DB/MapperTest.php @@ -120,7 +120,7 @@ class DBMapperTest extends \PHPUnit\Framework\TestCase public function testSelectById() { $query = - "SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(IFNULL(createdBy.salutation_name, ''), IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.last_name, ''))) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ". + "SELECT post.id AS `id`, post.name AS `name`, NULLIF(TRIM(CONCAT(IFNULL(createdBy.salutation_name, ''), IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.last_name, ''))), '') AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ". "FROM `post` ". "LEFT JOIN `user` AS `createdBy` ON post.created_by_id = createdBy.id " . "WHERE post.id = '1' AND post.deleted = '0'"; @@ -140,7 +140,7 @@ class DBMapperTest extends \PHPUnit\Framework\TestCase public function testSelect() { $query = - "SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(IFNULL(createdBy.salutation_name, ''), IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.last_name, ''))) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ". + "SELECT post.id AS `id`, post.name AS `name`, NULLIF(TRIM(CONCAT(IFNULL(createdBy.salutation_name, ''), IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.last_name, ''))), '') AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ". "FROM `post` ". "LEFT JOIN `user` AS `createdBy` ON post.created_by_id = createdBy.id " . "JOIN `post_tag` AS `tagsMiddle` ON post.id = tagsMiddle.post_id AND tagsMiddle.deleted = '0' ". @@ -290,7 +290,7 @@ class DBMapperTest extends \PHPUnit\Framework\TestCase public function testSelectRelatedBelongsTo() { $query = - "SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(IFNULL(createdBy.salutation_name, ''), IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.last_name, ''))) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ". + "SELECT post.id AS `id`, post.name AS `name`, NULLIF(TRIM(CONCAT(IFNULL(createdBy.salutation_name, ''), IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.last_name, ''))), '') AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ". "FROM `post` ". "LEFT JOIN `user` AS `createdBy` ON post.created_by_id = createdBy.id " . "WHERE post.id = '1' AND post.deleted = '0' ".