Advertisement
BenjaminS

TYPO3: PersistenceManager: Own query ordering

Dec 4th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. # Do your manual sorting after result set is retrieved
  2. # I'm using this on my API response which did not have a sorting possibility
  3.  
  4. $orderings = $query->getOrderings();
  5. if (!empty($orderings) && $results && count($results) > 1) {
  6.     usort($results, function ($a, $b) use ($orderings) {
  7.         $t = array(TRUE => -1, FALSE => 1);
  8.         $r = TRUE;
  9.         $k = 1;
  10.  
  11.         foreach ($orderings as $key => $direction) {
  12.             $k = (strtolower($direction) === 'asc') ? 1 : -1;
  13.             $key = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($key);
  14.  
  15.             $r = ($a[$key] < $b[$key]);
  16.             if ($a[$key] !== $b[$key]) {
  17.                 return $t[$r] * $k;
  18.             }
  19.         }
  20.         return $t[$r] * $k;
  21.     });
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement