Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorlovka/e729adf2abfd0d7ef63694e937afcf08 to your computer and use it in GitHub Desktop.
Save gorlovka/e729adf2abfd0d7ef63694e937afcf08 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)
));
// Should query min value too
$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