CakePHP Order By Rand()
Before I forget, I had to jot this quick discovery down. While not documented in the CakePHP manual (at least not anywhere I could find), it is possible to sort your associated models by RAND(). Maybe not earth shattering news to some people, but I find it interesting and handy.
So in your model file where you specify your association arrays, under the "order by" field you'll simply put "RAND()" instead of "name ASC" or "id ASC" etc.
Why on earth would you want to randomize the order of your associated models? I had to for a trivia application I was doing for work. I had a list of answers associated with questions.
So now the list of answer choices is in random order rather than by id or alphabetical. This makes it a little different for each user or each time you take the trivia. Makes it more challenging perhaps or not as easy to cheat. Also with the way the answers were originally ordered, the correct answer was showing up in the same spot over and over.
Sometimes the need to randomize is there and this is a very easy (quick) way of doing so in CakePHP.
Update: In case you didn't read the comments in this post, order by rand is not effecient and as I've read not as random as some other methods you can use. So order by rand may only be useful for you in some situtations. As with everything in the programming world there are many many ways to do things, just be sure to weigh out all your options and choose the best one.
CakePHP Order By Rand()
Before I forget, I had to jot this quick discovery down. While not documented in the CakePHP manual (at least not anywhere I could find), it is possible to sort your associated models by RAND(). Maybe not earth shattering news to some people, but I find it interesting and handy.
So in your model file where you specify your association arrays, under the "order by" field you'll simply put "RAND()" instead of "name ASC" or "id ASC" etc.
Why on earth would you want to randomize the order of your associated models? I had to for a trivia application I was doing for work. I had a list of answers associated with questions.
So now the list of answer choices is in random order rather than by id or alphabetical. This makes it a little different for each user or each time you take the trivia. Makes it more challenging perhaps or not as easy to cheat. Also with the way the answers were originally ordered, the correct answer was showing up in the same spot over and over.
Sometimes the need to randomize is there and this is a very easy (quick) way of doing so in CakePHP.
Update: In case you didn't read the comments in this post, order by rand is not effecient and as I've read not as random as some other methods you can use. So order by rand may only be useful for you in some situtations. As with everything in the programming world there are many many ways to do things, just be sure to weigh out all your options and choose the best one.
Hi Tom,
Order by RAND() is a bad idea. It’s a better idea to select a random result from an ordered result set, and if you need more than one, do so successively until you have the number of random results you might want. As mentioned in that (almost first google result for “order by rand”) reference, you can do what you like with a small table, but if it’s not small…
Cheers,
AD
Comment by AD7six — June 23, 2007 @ 9:05 am
No feedback in this comment form so I’ll try again
Google “order by rand” and you’ll find that for large db tables it’s the best way…. to tie up a database calculating random numbers.
Cheers,
AD
Comment by AD7six — June 23, 2007 @ 9:08 am
Thanks for the info. I did read a little bit about RAND() and did notice some concerns with it and not only that, but apparently it’s not so random. It was random enough for my small needs and again works in my case. I suppose with larger amounts of data it would not be so wise…but fortunately my application is talking about maybe 10-20 questions each with 3-4 answers…that’s 80 values that are being randomized at most. Not a big hit on the database and allows me to flexibly create “skins” or looks for the front end of the trivia that people take, in both HTML/PHP and Flash without the extra need to write code to sort through the associated arrays and restructure those arrays randomly. Or display the results randomly. However if things get larger I suppose this is a good thing to be aware of. You always seem to have good advice, thanks AD.
Comment by Tom — June 23, 2007 @ 5:38 pm