Skip to content

Instantly share code, notes, and snippets.

@SvenJuergens
Created December 9, 2019 10:20
Show Gist options
  • Save SvenJuergens/e70bdd42d43908ce2ef98a997e55c9d5 to your computer and use it in GitHub Desktop.
Save SvenJuergens/e70bdd42d43908ce2ef98a997e55c9d5 to your computer and use it in GitHub Desktop.
TYPO3 Order by Random im Repository
//hier gefunden:https://www.npostnik.de/typo3/typo3-order-by-random-im-repository/
/**
* @param int $limit
*/
public function findRandom($limit)
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->getTableName());
$queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
$records = $queryBuilder
->select('*')
->addSelectLiteral('rand() AS random_sort')
->from($this->getTableName())
->orderBy('random_sort')
->setMaxResults($limit)
->execute()
->fetchAll();
return records;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment