Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric order works on MySQL databases #1778

Merged
merged 4 commits into from
Aug 26, 2020
Merged
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
1 change: 0 additions & 1 deletion config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ doctrine:
string_functions:
JSON_EXTRACT: Bolt\Doctrine\Functions\JsonExtract
CAST: DoctrineExtensions\Query\Mysql\Cast
INSTR: DoctrineExtensions\Query\Mysql\Instr
numeric_functions:
RAND: Bolt\Doctrine\Functions\Rand

4 changes: 2 additions & 2 deletions src/Doctrine/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public function hasJson(): bool
return false;
}

public function hasInstrAndCast(): bool
public function hasCast(): bool
{
try {
$query = $this->connection->createQueryBuilder();
$query
->select('CAST (1.1 AS int), INSTR("Bolt", "o")');
->select('CAST (1.1 AS int)');
$query->execute();
} catch (\Throwable $e) {
return false;
Expand Down
7 changes: 7 additions & 0 deletions src/Entity/Field/NumberField.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@
class NumberField extends Field implements FieldInterface, ScalarCastable
{
public const TYPE = 'number';

public function getValue(): ?array
{
$pv = parent::getValue();

return empty($pv) ? $pv : [(float) $pv[0]];
}
}
14 changes: 5 additions & 9 deletions src/Storage/Directive/OrderDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,20 @@ private function orderByNumericField(QueryInterface $query, string $translations
{
$qb = $query->getQueryBuilder();

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

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

return;
}

$qb->addSelect('INSTR(' . $translationsAlias . '.value, \'%[0-9]%\') as HIDDEN instr');
$innerSubstring = $qb
$substring = $qb
->expr()
->substring($translationsAlias . '.value', 'instr', $qb->expr()->length($translationsAlias . '.value'));
$outerSubstring = $qb
->expr()
->substring($innerSubstring, 3, $query->getQueryBuilder()->expr()->length($translationsAlias . '.value'));
$qb->addOrderBy('CAST(' . $outerSubstring . ' as decimal) ', $direction);
->substring($translationsAlias . '.value', 3, $query->getQueryBuilder()->expr()->length($translationsAlias . '.value'));
$qb->addOrderBy('CAST(' . $substring . ' as decimal) ', $direction);
}

private function isNumericField(QueryInterface $query, $fieldname): bool
Expand Down