diff --git a/src/Actions/Select.php b/src/Actions/Select.php index 53c7cd7..f81ce67 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; + return 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; + } } 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; }