Cakephp 1.3 如何从模型中返回单个随机行?

Cakephp 1.3 如何从模型中返回单个随机行?,cakephp-1.3,Cakephp 1.3,尝试从模型中获取单个随机行。我从网上下载了这个: $this->Testimonial->findAll(null,null,'rand()',1,null,null); 不幸的是,cakephp 1.3中不再存在findAll。您可以尝试以下方法: $count = $this->Testimonial->find('count'); $this->Testimonial->find('first', array('conditions' => ar

尝试从模型中获取单个随机行。我从网上下载了这个:

$this->Testimonial->findAll(null,null,'rand()',1,null,null);
不幸的是,cakephp 1.3中不再存在findAll。您可以尝试以下方法:

$count = $this->Testimonial->find('count');
$this->Testimonial->find('first', array('conditions' => array('id' => rand(1,$count))));

(这也不会检索“所有”结果)

如果我的ID不是按顺序排列的呢?那么你只需检索所有结果,然后随机选择一个,
$find=$this->Model->find('all');echo$find[rand(0,count($find))]['Model']['someName']
@Thomas:这浪费了很多资源,因为所有数据都必须从数据库放到PHP中才能扔掉。最好让DB服务器处理正确数据集的选取。
$this->Quote->find('first', array('order' => array('rand()')))