Commit 5f1184b8 authored by Bernhard Schussek's avatar Bernhard Schussek

Merge branch 'bugfix/sorting' into 'master'

Fixed cursor navigation when using multiple sort fields with different default directions

See merge request !8
parents f20563e1 72e269e3
......@@ -94,23 +94,27 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
*/
public function addSearchFromOffset(QueryBuilder $qb, string $alias, Offset $fromOffset, string $sortDirection): void
{
$invertDirections = $sortDirection !== $this->defaultDirections[0];
$fieldValues = $this->split($fromOffset);
$nextConditionPrefixes = [];
$i = 0;
$lastIndex = count($fieldValues) - 1;
foreach ($fieldValues as $fieldValue) {
foreach ($fieldValues as $i => $fieldValue) {
$fieldName = $this->fieldNames[$i];
$fieldDirection = $invertDirections
? OrderDirection::invert($this->defaultDirections[$i])
: $this->defaultDirections[$i];
$conditionPrefix = count($nextConditionPrefixes) > 0
? implode(' AND ', $nextConditionPrefixes).' AND '
: ''
;
: '';
$qb->orWhere($conditionPrefix.sprintf(
'%s %s :fromOffset%s',
$this->addAlias($this->fieldNames[$i], $alias),
$this->addAlias($fieldName, $alias),
$i === $lastIndex
? (OrderDirection::ASCENDING === $sortDirection ? '>=' : '<=')
: (OrderDirection::ASCENDING === $sortDirection ? '>' : '<'),
? (OrderDirection::ASCENDING === $fieldDirection ? '>=' : '<=')
: (OrderDirection::ASCENDING === $fieldDirection ? '>' : '<'),
$i
));
......@@ -121,11 +125,9 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
$nextConditionPrefixes[] = sprintf(
'%s = :fromOffset%s',
$this->addAlias($this->fieldNames[$i], $alias),
$this->addAlias($fieldName, $alias),
$i
);
++$i;
}
}
......@@ -134,20 +136,24 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
*/
public function addSearchAfterOffset(QueryBuilder $qb, string $alias, Offset $afterOffset, string $sortDirection): void
{
$invertDirections = $sortDirection !== $this->defaultDirections[0];
$fieldValues = $this->split($afterOffset);
$nextConditionPrefixes = [];
$i = 0;
foreach ($fieldValues as $fieldValue) {
foreach ($fieldValues as $i => $fieldValue) {
$fieldName = $this->fieldNames[$i];
$fieldDirection = $invertDirections
? OrderDirection::invert($this->defaultDirections[$i])
: $this->defaultDirections[$i];
$conditionPrefix = count($nextConditionPrefixes) > 0
? implode(' AND ', $nextConditionPrefixes).' AND '
: ''
;
: '';
$qb->orWhere($conditionPrefix.sprintf(
'%s %s :afterOffset%s',
$this->addAlias($this->fieldNames[$i], $alias),
OrderDirection::ASCENDING === $sortDirection ? '>' : '<',
$this->addAlias($fieldName, $alias),
OrderDirection::ASCENDING === $fieldDirection ? '>' : '<',
$i
));
......@@ -158,11 +164,9 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
$nextConditionPrefixes[] = sprintf(
'%s = :afterOffset%s',
$this->addAlias($this->fieldNames[$i], $alias),
$this->addAlias($fieldName, $alias),
$i
);
++$i;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment