We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to read a random database or multiple

I need to read a random record from the database, how to write

$model = new articles(); $model::findFirst(array('order'="??????????") );



4.7k
Accepted
answer

One way,especially if the table is fairly small, you can order by RAND(). But the way you initiated the modal the second line would be: $model->findFirst(array('order' => 'RAND()'));

Another method for larger data would be to find max id, if you have auto increase integer id, then select a random number using PHP function.