3
尝试从模型中获取单个随机行。在CakePHP的1.3如何从模型中返回一个随机行?
尝试从模型中获取单个随机行。在CakePHP的1.3如何从模型中返回一个随机行?
$this->Quote->find('first', array('order' => array('rand()')))
$this->Testimonial->findAll(null,null,'rand()',1,null,null);
不幸的findAll不存在了,那么你可以试试这个:我把这个网址
$count = $this->Testimonial->find('count');
$this->Testimonial->find('first', array('conditions' => array('id' => rand(1,$count))));
(这也不会检索 “全”结果)
如果我的ID不是连续的顺序会怎样? – 2011-03-11 15:47:40
然后你只是检索所有的结果,然后随机选择一个, '$ find = $ this-> Model-> find('all'); echo $ find [rand(0,count($ find))] ['Model'] ['someName']' – Thomas 2011-03-29 14:56:30
@Thomas:这会浪费很多资源,因为所有数据都必须从数据库PHP只是为了扔掉。最好让数据库服务器处理挑选正确的数据集。 – 2011-08-17 13:26:54