Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return a single random row from a model?

Tags:

cakephp-1.3

Trying to get a single random row from a model. I pulled this from the web:

$this->Testimonial->findAll(null,null,'rand()',1,null,null);

Unfortunately findAll doesn't exist anymore in cakephp 1.3

like image 493
Jack B Nimble Avatar asked Feb 09 '26 15:02

Jack B Nimble


2 Answers

$this->Quote->find('first', array('order' => array('rand()')))
like image 85
Martin Ueding Avatar answered Feb 15 '26 21:02

Martin Ueding


you can try this:

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

(this also doesn't retrieve "all" the results)

like image 25
Thomas Avatar answered Feb 15 '26 20:02

Thomas