From f07f9fbc0ca91ce5bb96e1b7b82d9b64ad6a2e5f Mon Sep 17 00:00:00 2001 From: Artem Prozorov Date: Thu, 8 Apr 2021 15:06:08 +0300 Subject: [PATCH 1/3] Added fixes for order by when no entity id must be returned --- src/Actions/Select.php | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Actions/Select.php b/src/Actions/Select.php index 53c7cd7..33bab7e 100644 --- a/src/Actions/Select.php +++ b/src/Actions/Select.php @@ -48,6 +48,16 @@ class Select extends AbstractAction */ protected string $searchString = ''; + /** + * @var bool + */ + private bool $useCustomOrderBy = false; + + /** + * @var string + */ + private string $customOrderBy = ''; + /** * @param Join $join * @return $this|void @@ -263,9 +273,15 @@ protected function buildSorting(): string ); } - $default = sprintf('ORDER BY _%d.id ASC', $this->baseEntityId); + if (count($chunks)) { + return sprintf('ORDER BY %s', join(',', $chunks)); + } + + if ($this->useCustomOrderBy === true) { + return "ORDER BY {$this->customOrderBy} ASC"; + } - return count($chunks) ? sprintf('ORDER BY %s', join(',', $chunks)) : $default; + $default = sprintf("ORDER BY _%d.id ASC", $this->baseEntityId); } /** @@ -283,4 +299,15 @@ public function getResultColumnsMeta(): array { return $this->getReturningColumnsMeta(); } + + /** + * @param string $orderBy + * @return void + */ + protected function setCustomOrderBy(string $orderBy): void + { + $this->useCustomOrderBy = true; + + $this->customOrderBy = $orderBy; + } } From d7d983e7079c830a968de80d2779e53dc402de66 Mon Sep 17 00:00:00 2001 From: Artem Prozorov Date: Thu, 8 Apr 2021 15:08:59 +0300 Subject: [PATCH 2/3] Fixed typo --- src/Actions/Select.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Actions/Select.php b/src/Actions/Select.php index 33bab7e..f81ce67 100644 --- a/src/Actions/Select.php +++ b/src/Actions/Select.php @@ -281,7 +281,7 @@ protected function buildSorting(): string return "ORDER BY {$this->customOrderBy} ASC"; } - $default = sprintf("ORDER BY _%d.id ASC", $this->baseEntityId); + return sprintf("ORDER BY _%d.id ASC", $this->baseEntityId); } /** From 03e2ca04bbd2f6a858cc13d7bb1d01d50fe64cf4 Mon Sep 17 00:00:00 2001 From: Artem Prozorov Date: Thu, 8 Apr 2021 15:10:58 +0300 Subject: [PATCH 3/3] Added setCustomOrderBy in ReturningAwareTrait --- src/Traits/ReturningAwareTrait.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Traits/ReturningAwareTrait.php b/src/Traits/ReturningAwareTrait.php index 064616c..c0d4e89 100644 --- a/src/Traits/ReturningAwareTrait.php +++ b/src/Traits/ReturningAwareTrait.php @@ -125,6 +125,8 @@ protected function buildReturning(): string ) ); } + + $this->setCustomOrderBy('row_id'); $chunks[0] = '1 AS row_id'; $chunks[] = sprintf('%s(%s) AS %s', $attributeAggFunction, $this->columnExpression($attribute), $attributeAlias); $this->registerReturningAttribute(new ResultColumnMeta($attributeAlias, $attribute, $attributeAggFunction)); @@ -244,4 +246,10 @@ private function getReturningColumnsMeta(): array { return array_values($this->returningColumnsMeta); } + + /** + * @param string $orderBy + * @return void + */ + abstract protected function setCustomOrderBy(string $orderBy): void; }