typo3 query: ORDER BY RAND()

I needed a query that returns 5 random items. My solution:

//fetch 5 IDs from the mm table
$idArr = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( 'uid_local', 'tx_something_user_userarticles_article_mm','', '', 'RAND()', '5' );
$ids = array();
//save the IDs to $ids
foreach($idArr as $k => $v){
	$ids[] = $v['uid_local'];
}
//now for the real query
$query = $this->createQuery();
$constraints = array(
	$query->in('uid', $ids),
);
$query->matching($query->logicalAnd($constraints));
return $query->execute(); 

It works. But there may be better solutions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.