[TYPO3-mvc] Random sorting without custom query?
Jochen Rau
jochen.rau at typoplanet.de
Wed Jul 21 13:21:13 CEST 2010
Hi Franz.
On 2010-07-21, Franz Koch <typo3.RemoveForMessage at elements-net.de> wrote:
> 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();
Nice workaround! (although parseQuery() is no official API)
Regards
Jochen
More information about the TYPO3-project-typo3v4mvc
mailing list