[TYPO3-mvc] Random sorting without custom query?

Franz Koch typo3.RemoveForMessage at elements-net.de
Wed Jul 21 12:11:57 CEST 2010


Hi,

> is it in any way possible to set an ordering like "RAND()" with the
> query builder methods? If I use the standard setOrderings I get an error
> that the field "RAND()" doesn't exist. Which is no surprise. But I don't
> want to create a complete custom sql query just to implement a random
> sorting.
>
> Any hints are welcome

I was facing the same problem once and also didn't want to build all the 
possible constraints by my own when the QOM is doing such a good job at 
it - so I did a little research and found kind of a "clean" hack :)

$query = $this->createQuery();
// ... do here whatever you like with the QOM

// let's start hacking
// have the storage backend build the final SQL statement and manipulate it
$backend = 
t3lib_div::makeInstance('Tx_Extbase_Persistence_Storage_Typo3DbBackend');
$parameters = array();

// have the storage backend create the statement parts
$statementParts = $backend->parseQuery($query, $parameters);			

// manipulate the statement parts as you need them
$statementParts['orderings']['rand'] = ' RAND(NOW())';

// build the final statement and pass it to the query object
$statement = $backend->buildQuery($statementParts, $parameters);
$query->statement($statement, $parameters);

// enjoy
return $query->execute();

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list