Skip to content

Commit

Permalink
item::random_query() doesn't need to take a "where" clause because
Browse files Browse the repository at this point in the history
it's returning a query, so the caller can add the where clause
himself.  This makes for a cleaner API.
  • Loading branch information
bharat committed Jun 6, 2010
1 parent 87fde3f commit 4e56176
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
5 changes: 1 addition & 4 deletions modules/gallery/helpers/item.php
Expand Up @@ -209,17 +209,14 @@ static function root() {

/**
* Return a query to get a random Item_Model, with optional filters
*
* @param array (optional) where tuple
*/
static function random_query($where=null) {
static function random_query() {
// Pick a random number and find the item that's got nearest smaller number.
// This approach works best when the random numbers in the system are roughly evenly
// distributed so this is going to be more efficient with larger data sets.
return ORM::factory("item")
->viewable()
->where("rand_key", "<", ((float)mt_rand()) / (float)mt_getrandmax())
->merge_where($where)
->order_by("rand_key", "DESC");
}
}
2 changes: 1 addition & 1 deletion modules/image_block/helpers/image_block_block.php
Expand Up @@ -31,7 +31,7 @@ static function get($block_id, $theme) {
// @todo Consider another fallback if further optimizations are necessary.
$attempts = 0;
do {
$item = item::random_query(array(array("type", "!=", "album")))->find_all(1)->current();
$item = item::random_query()->where("type", "!=", "album")->find_all(1)->current();
} while (!$item && $attempts++ < 3);
if ($item && $item->loaded()) {
$block = new Block();
Expand Down

0 comments on commit 4e56176

Please sign in to comment.