From 0276fcf5cef2cb5b0ad0c3c2c20fe17eed9cac44 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 23 May 2023 14:24:43 +0300 Subject: [PATCH] orm fix select item array with 1 element --- .../ORM/QueryComposer/BaseQueryComposer.php | 11 ++++++++--- tests/unit/Espo/ORM/MysqlQueryComposerTest.php | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/application/Espo/ORM/QueryComposer/BaseQueryComposer.php b/application/Espo/ORM/QueryComposer/BaseQueryComposer.php index 1ebad73700..c5e5474189 100644 --- a/application/Espo/ORM/QueryComposer/BaseQueryComposer.php +++ b/application/Espo/ORM/QueryComposer/BaseQueryComposer.php @@ -1813,9 +1813,14 @@ abstract class BaseQueryComposer implements QueryComposer ]; } - if (is_array($attribute) && count($attribute) === 2) { - $alias = $attribute[1]; + if (is_array($attribute) && count($attribute) === 1) { + $attribute = $attribute[0]; + } + if (is_array($attribute) && count($attribute) === 2) { + // @todo Refactor to unite convertComplexExpression and select, noSelect, notStorable (here and below). + + $alias = $attribute[1]; $attribute0 = $attribute[0]; if (!$entity->hasAttribute($attribute0)) { @@ -1848,7 +1853,7 @@ abstract class BaseQueryComposer implements QueryComposer } if (!is_string($attribute)) { - throw new RuntimeException(); + throw new RuntimeException("Bad select."); } if (!$entity->hasAttribute($attribute)) { diff --git a/tests/unit/Espo/ORM/MysqlQueryComposerTest.php b/tests/unit/Espo/ORM/MysqlQueryComposerTest.php index 75d8bd4aaf..1bb2be770d 100644 --- a/tests/unit/Espo/ORM/MysqlQueryComposerTest.php +++ b/tests/unit/Espo/ORM/MysqlQueryComposerTest.php @@ -3128,4 +3128,22 @@ class MysqlQueryComposerTest extends \PHPUnit\Framework\TestCase $this->query->composeSelect($query) ); } + + public function testSelections1(): void + { + $query1 = SelectBuilder::create() + ->select(['number']) + ->from('Post') + ->build(); + + $query2 = SelectBuilder::create() + ->select($query1->getSelect()) + ->from('Post') + ->build(); + + $this->assertEquals( + $this->query->composeSelect($query1), + $this->query->composeSelect($query2) + ); + } }