Skip to content

Commit

Permalink
Merge pull request #1765 from bolt/fix/numeric-sort-for-older-php
Browse files Browse the repository at this point in the history
Fall back to "dumb" numeric sorting for older SQLite versions
  • Loading branch information
I-Valchev authored Aug 25, 2020
2 parents feb986c + 0fc32e9 commit 85c44d3
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 85c44d3

Please sign in to comment.