[TYPO3-mvc] own sorting with extbase
Philipp
philippwrann at gmx.at
Fri Jan 24 13:50:51 CET 2014
I managed this by overloading the Typo3DbBackend Class
Can be done via Typoscript
There is extended the method parseOrderings for that purpose.
protected function parseOrderings(array $orderings, \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source, array &$sql) {
foreach ($orderings as $propertyName => $order) {
switch ($order) {
case \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING:
case \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING:
$order = 'ASC';
break;
case \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING:
case \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING:
$order = 'DESC';
break;
default:
throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedOrderException('Unsupported order encountered.', 1242816074);
}
$className = '';
$tableName = '';
/**
* MODIFICATION START
* This way we enable the use of a function inside ordering
* this is mysql specific but its needed for our listMode Priority feature
* This way we can get a ordered list but put some specific objects on top of the list
*/
if (stripos($propertyName,'FIELD(') !== FALSE) {
$parts = explode(',',str_replace(')','',$propertyName));
$property = str_ireplace('FIELD(','',array_shift($parts));
$values = array_filter(array_filter($parts,function($value){
return !preg_match_all("/[^\w]/",$value);
}));
$className = $source->getNodeTypeName();
$tableName = $this->dataMapper->convertClassNameToTableName($className);
$columnName = $this->dataMapper->convertPropertyNameToColumnName($property, $className);
$fieldString = 'FIELD('.$tableName.'.'.$columnName.','.implode(',',$values).')';
$sql['orderings'][] = $fieldString. ' ' . $order;
} else {
/**
* MODIFICATION END
*/
if ($source instanceof \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SelectorInterface) {
$className = $source->getNodeTypeName();
$tableName = $this->dataMapper->convertClassNameToTableName($className);
while (strpos($propertyName, '.') !== FALSE) {
$this->addUnionStatement($className, $tableName, $propertyName, $sql);
}
} elseif ($source instanceof \TYPO3\CMS\Extbase\Persistence\Generic\Qom\JoinInterface) {
$tableName = $source->getLeft()->getSelectorName();
}
$columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
if (strlen($tableName) > 0) {
$sql['orderings'][] = $tableName . '.' . $columnName . ' ' . $order;
} else {
$sql['orderings'][] = $columnName . ' ' . $order;
}
}
}
does the job but of course its not the best way.
More information about the TYPO3-project-typo3v4mvc
mailing list