【发布时间】:2011-01-29 08:17:10
【问题描述】:
我想使用 CakePHP 从我的数据库中随机抽取数据样本。这是我的功能:
function categories_list()
{
$this->paginate['limit'] = 6;
$this->paginate['order'] = '';
$this->paginate['conditions'] = '';
// Sort Randomly Start
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);
// Sort Randomly End
$this->set('cat_ajax_items', $this->paginate('Category'));
}
问题是,Cake 发送给 DB 的查询总是 this 到 RAND() 部分,从而使 MySQL 陷入困境:
ORDER BY RAND(`1235123412341`)
在手动查询上进行测试,它工作得很好,并且当它的格式如下时返回一个样本:
ORDER BY RAND(1235123412341)
有什么方法可以让 Cake 退出自动格式化?我放入该 RAND() 函数的任何内容都会转储到字符串引号中。
【问题讨论】: