Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Actions/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
}
8 changes: 8 additions & 0 deletions src/Traits/ReturningAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}