[TYPO3-mvc] Sorting of StorageObject's with extended foreign_sortby field

Tim Schoch | GSTALTIG tim.schoch at gstaltig.ch
Thu Oct 28 15:43:12 CEST 2010


Hello
 
By default, the TCA foreign_sortby field is sorted ascending. This is hardcoded in Tx_Extbase_Persistence_Mapper_DataMapper:
$query->setOrderings(array($columnMap->getChildSortByFieldName() => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING));
 
For a current project I need both ascending and descending sorting, so I added some lines of code:
if ( preg_match( '#(.*?) (DESC|ASC)?$#i', $columnMap->getChildSortByFieldName(), $field ) ) {
    if ( 'DESC' == strtoupper( $field[2]) ) {
        $ordering = Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING;    
    } else {
        $ordering = Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING;    
    }
    $query->setOrderings(array( $field[1] => $ordering ));
} else {
    $query->setOrderings(array($columnMap->getChildSortByFieldName() => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING));  
}
 
Now I add the sorting in the TCA's foreign_sortby field like this:
'foreign_sortby' => 'buyDate DESC'
 
I don't know, if there are any drawbacks with it. I'm using it with MySQL and in the FE only.
Maybe the Devs come up with a better way of specifing the sorting order, but for now it does the job.
 
Gruess Tim
 
GSTALTIG GMBH / www.gstaltig.ch
Sonnenhofstr. 13 / 6020 Emmenbrücke
tim at gstaltig.ch / 076 488 24 97     
 


More information about the TYPO3-project-typo3v4mvc mailing list