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

Allow order by number field correctly #1616

Merged
merged 3 commits into from
Jul 10, 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: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ext-pdo": "*",
"api-platform/core": "^2.5",
"babdev/pagerfanta-bundle": "^2.4",
"beberlei/doctrineextensions": "^1.0",
"bolt/common": "^2.1.6",
"cocur/slugify": "^4.0",
"composer/composer": "^1.10",
Expand Down
3 changes: 3 additions & 0 deletions config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ doctrine:
string_functions:
JSON_EXTRACT: Bolt\Doctrine\Functions\JsonExtract
JSON_CONTAINS: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonContains
CAST: DoctrineExtensions\Query\Mysql\Cast
INSTR: DoctrineExtensions\Query\Mysql\Instr
numeric_functions:
RAND: Bolt\Doctrine\Functions\Rand

5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ parameters:
message: '#Function twig_date_format_filter not found.#'
path: %currentWorkingDirectory%/src/Twig/FieldExtension.php

# Parameters in Storage\Directive\OrderDirective::orderByNumericField() aren't seen as ints
-
message: '#of method Doctrine\\ORM\\Query\\Expr::substring\(\) expects int#'
path: %currentWorkingDirectory%/src/Storage/Directive/OrderDirective.php

includes:
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
Expand Down
14 changes: 14 additions & 0 deletions public/theme/skeleton/custom/setcontent_1.twig
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,18 @@
<section>
#}

<section id="nine">
<h1>Nine</h1>
{% setcontent showcases = 'showcases' orderby '-floatfield' %}

<ul>
{% for showcase in showcases %}
<li>showcase {{ showcase.id }}: {{ showcase.floatfield }}
<span class="s{{ loop.index }}">{{ _self.issmaller(showcase.floatfield, last|default()) }}</span>
</li>
{% set last = showcase.floatfield %}
{% endfor %}
</ul>
</section>

{% endblock main %}
2 changes: 1 addition & 1 deletion src/DataFixtures/ContentFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private function getValuesforFieldType(string $name, DeepCollection $field, bool

break;
case 'number':
$data = [$this->faker->numberBetween(-100, 1000)];
$data = [(string) $this->faker->numberBetween(-100, 1000)];

break;
case 'checkbox':
Expand Down
34 changes: 31 additions & 3 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\Entity\Field\NumberField;
use Bolt\Storage\QueryInterface;
use Bolt\Twig\Notifications;
use Bolt\Utils\ContentHelper;
Expand Down Expand Up @@ -87,18 +88,24 @@ private function setOrderBy(QueryInterface $query, string $order, string $direct
$fieldAlias = 'order_' . $query->getIndex();
$translationsAlias = 'translations_order_' . $query->getIndex();

// Note the `lower()` in the `addOrderBy()`. It is essential to sorting the
// results correctly. See also https://github.com/bolt/core/issues/1190
$query
->getQueryBuilder()
->leftJoin('content.fields', $fieldsAlias)
->leftJoin($fieldsAlias . '.translations', $translationsAlias)
->andWhere($fieldsAlias . '.name = :' . $fieldAlias)
->andWhere($translationsAlias . '.locale = :' . $fieldAlias . '_locale')
->setParameter($fieldAlias . '_locale', $locale)
->addOrderBy('lower(' . $translationsAlias . '.value)', $direction)
->setParameter($fieldAlias, $order);

if ($this->isNumericField($query, $order)) {
$this->orderByNumericField($query, $translationsAlias, $direction);
} else {
// Note the `lower()` in the `addOrderBy()`. It is essential to sorting the
// results correctly. See also https://github.com/bolt/core/issues/1190
$query
->getQueryBuilder()
->addOrderBy('lower(' . $translationsAlias . '.value)', $direction);
}
$query->incrementIndex();
}
}
Expand Down Expand Up @@ -151,4 +158,25 @@ private function getTitleFormat(QueryInterface $query): ?string

return $contentType->get('title_format', null);
}

private function orderByNumericField(QueryInterface $query, string $translationsAlias, string $direction): void
{
$qb = $query->getQueryBuilder();
$qb->addSelect('INSTR(' . $translationsAlias . '.value, \'%[0-9]%\') as HIDDEN instr');
$innerSubstring = $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);
}

private function isNumericField(QueryInterface $query, $fieldname): bool
{
$contentType = $query->getConfig()->get('contenttypes/' . $query->getContentType());
$type = $contentType->get('fields')->get($fieldname)->get('type', false);

return $type === NumberField::TYPE;
}
}