[TYPO3-mvc] Random sorting without custom query?

Tobias Pinnekamp typo3 at tpinne.de
Tue Jul 20 21:19:36 CEST 2010


Hi Felix,

Am 20.07.2010 20:11 Uhr, schrieb Felix Oertel:
> Hi Tobias,
>
> Am 20.07.10 15:39, schrieb Tobias Pinnekamp:
>> is it in any way possible to set an ordering like "RAND()" with the
>> query builder methods?
>
> I think it's not possible at the moment. We would have to introduce a
> constant for that but this would raise some problems as for now the
> orderings are bound to a field and random would not.
>
> In general the query-object is nice to do most of the stuff you have to
> do most of the time, but it can't (and doesn't want to) cover
> everybody's needs. I think it's ok, to use a custom query here.
>
> I personally would prefer to shuffle() the array afterwards and keep my
> query clean.
>
> Someone else into this? Do we need this? I just needed it once and used
> custom query anyway.
>
> regards, foertel

at the moment I implemented it using shuffle(). But this is not the best 
solution if you want to get random 5 objects out of 100. With SQL you 
only load the 5 needed, with shuffle you always load everything and 
throw 95% away.

The other "problem", which produces duplicate code is, if you create the 
query depending on several options which can be dynamically set. Here 
the query-builder is very useful to generate the query. But if you want 
to use something like "RAND()" (or any other non-field-construct) you 
con throw that away and must build instead a raw sql-query. And building 
a complete raw sql-query depending on the options is then very pi_base.

In my opinion such a possibility would be helpful. Not only to use 
RAND() for sorting, it would also be nice if one could use a 
sql-function in the where-building part. So that it is not in general 
bound to a field. I don't know if this is implementable in an easy and 
unoverloaded way.

Don't get me wrong. I love the new way to handle the database interaction.

Maybe there will be some discussion on this. Until that I will check 
reimplement it as a raw-sql query.


Thx,
Tobias


More information about the TYPO3-project-typo3v4mvc mailing list