Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Forked from hugodotlau/gist:4031109
Last active November 10, 2016 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vyspiansky/9596632 to your computer and use it in GitHub Desktop.
Save vyspiansky/9596632 to your computer and use it in GitHub Desktop.
Yii: how can I get random values from model
<?php
// Source: http://goo.gl/NOCJKM
$max = Table::model()->count(array(
'condition' => 'status<:status',
'params' => array('status' => Table::STATUS_A)
));
$offset = rand(0, $max);
$object = Table::model()->find(array(
'condition' => 'status<:status',
'params' => array('status' => Table::STATUS_A),
'offset' => $offset,
'limit' => 1
));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment