I want to get a random selection of data from my database using CakePHP. Here is my function:
function categories_list()
{
$this->paginate['limit'] = 6;
$this->paginate['order'] = '';
$this->paginate['conditions'] = '';
if ($this->Session->check('Category.randomSeed'))
{
$seed = $this->Session->read('Category.randomSeed');
} else {
$seed = mt_rand();
$this->Session->write('Category.randomSeed', $seed);
}
$this->paginate['order'] = sprintf('RAND(%d)', $seed);
$this->set('cat_ajax_items', $this->paginate('Category'));
}
The problem is that the request that Cake sends to the database always does this with the RAND () part, sending MySQL in a flexible form:
ORDER BY RAND(`1235123412341`)
Testing by manual request, it works very well and returns a sample when it is formatted as follows:
ORDER BY RAND(1235123412341)
Is there a way to make Cake refuse autoformatting? Everything that I put into this RAND () function is reset to string quotes.
source
share