Skip to content

Instantly share code, notes, and snippets.

@dmitryd
Created December 18, 2018 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitryd/ab4a53bb11b81993e3897f9644604a45 to your computer and use it in GitHub Desktop.
Save dmitryd/ab4a53bb11b81993e3897f9644604a45 to your computer and use it in GitHub Desktop.
<?php
namespace Vendor\Extension\Storage;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser;
/**
* This class allows ordering by 'FIELD' SQL statement.
*
* @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
* @see https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_field
*/
class Typo3DbQueryParseWithOrderByField extends Typo3DbQueryParser
{
/**
* Adds FIELD sorting support to the query.
*
* @param array $orderings
* @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source
* @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedOrderException
*/
protected function parseOrderings(array $orderings, SourceInterface $source)
{
foreach ($orderings as $propertyName => $order) {
if (preg_match('/^\s*field\s*\(/i', $propertyName)) {
$this->queryBuilder->getConcreteQueryBuilder()->addOrderBy($propertyName, $order);
} else {
parent::parseOrderings([$propertyName => $order], $source);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment