I am trying to assign a defined random order to a table of over 10000+ records. I have a function utilizing a start date and adding 1 second to each consecutive date, assigned randomly. Then I could sort by the random assigned date. My function worked fine with 50 records, but fails with 10000+ records.
It sets correct dates for about 9000 records, but 1146 records get assigned 0 (1969-12-31 19:00:00) Any help getting this or something similar to work would be appreciated.
function randomize(){
$count = $this->Application->find('count');
$order = range(0, $count-1); // Array of numbers 0 to count-1
$startDate = strtotime('December 13, 2011 0:00:00');
shuffle($order); // scramble array of numbers
$Applications = $this->Application->find('all');
set_time_limit(0);
foreach($Applications as $app){
$this->Application->id = $app['Application']['id'];
$this->Application->saveField('order', date('Y-m-d H:i:s', $startDate + $order[$this->Application->id]));
}
set_time_limit(30);
}
Update: I am using MySQL database but need a permanent state for 1 randomization, not repeated randomization as per ORDER BY RAND(). I also updated the code (see above) to reduce overhead, and increased memory in php.ini from 128M to 256M. With the code change the bad dates are no longer 0 but the same as $startDate indicating it may be an issue with the $order array of numbers.
Questions:
Are you sure you’re using the proper date format there?
Why a start date to randomize? Take the date as a fixed number, as you’re doing (X in this case): If you do
X + givenOrderNumberfor each record then the order will be defined bygivenOrderNumber… so why the unnecessary addition?I’ve got the query I understand you’re looking for here:
Example
It sorts the records by a date which is incremented by one each time. Now if you want to use the application id cheat:
Example
However, whether this is useful or not for you… it seems to be unnecessary.
Hope this helps.