Forked from vyspiansky/random-record-from-model.php
Created
November 10, 2016 06:44
-
-
Save gorlovka/e729adf2abfd0d7ef63694e937afcf08 to your computer and use it in GitHub Desktop.
Yii: how can I get random values from model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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