Skip to content

Commit

Permalink
Fall back to "dumb" numeric sorting for older SQLite versions
Browse files Browse the repository at this point in the history
  • Loading branch information
bobdenotter committed Aug 24, 2020
1 parent 0932a98 commit 4ce4a5d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Storage/Directive/OrderDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bolt\Storage\Directive;

use Bolt\Doctrine\Version;
use Bolt\Entity\Field\NumberField;
use Bolt\Storage\QueryInterface;
use Bolt\Twig\Notifications;
Expand Down Expand Up @@ -170,6 +171,17 @@ private function getTitleFormat(QueryInterface $query): ?string
private function orderByNumericField(QueryInterface $query, string $translationsAlias, string $direction): void
{
$qb = $query->getQueryBuilder();

// For older bundled SQLite in PHP 7.2 that do not have `INSTR` and `CAST` built in, we fall back to the
// "dumb" sorting instead. For this we use the same criteria as to check whether we have JSON. C'est la vie.
$doctrineVersion = new Version($query->getQueryBuilder()->getEntityManager()->getConnection());

if (! $doctrineVersion->hasJson()) {
$qb->addOrderBy($translationsAlias . '.value', $direction);

return;
}

$qb->addSelect('INSTR(' . $translationsAlias . '.value, \'%[0-9]%\') as HIDDEN instr');
$innerSubstring = $qb
->expr()
Expand Down

0 comments on commit 4ce4a5d

Please sign in to comment.