Created
November 22, 2010 11:04
-
-
Save dogmatic69/709822 to your computer and use it in GitHub Desktop.
get random rows from a db
This file contains 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
class RandomBehavior extends ModelBehavior | |
{ | |
public $name = 'Random'; | |
/** | |
* field : (string) The field to be ordered by. | |
* | |
* foreign_key : (string) The field to identify one SET by. | |
* Each set has their own order (ie they start at 1). | |
* Set to FALSE to not use this feature (and use only 1 set) | |
*/ | |
public $settings = array( | |
'Ad' => array( | |
'conditions' => array( | |
), | |
'field' => 'id', | |
'limit' => 5 | |
) | |
); | |
function setup( &$Model, $settings ) | |
{ | |
$this->settings[$Model->alias] = array_merge( | |
$this->settings[$Model->alias], | |
(array)$settings | |
); | |
$this->settings[$Model->alias]['model'] = $Model->alias; | |
$this->settings[$Model->alias]['primaryKey'] = $Model->primaryKey; | |
$this->settings[$Model->alias]['cache_file'] = 'Random_'.$this->settings[$Model->alias]['model'].'_'.$this->settings[$Model->alias]['primaryKey']; | |
if ( ( $this->settings[$Model->alias]['count'] = Cache::read( $this->settings[$Model->alias]['cache_file'] ) ) === false ) | |
{ | |
$Model->recursive = -1; | |
$this->settings[$Model->alias]['count'] = $Model->find( | |
'count', | |
array( | |
'conditions' => $this->settings[$Model->alias]['conditions'] | |
) | |
); | |
Cache::write( $this->settings[$Model->alias]['cache_file'], $this->settings[$Model->alias]['count'] ); | |
} | |
if ( $this->settings[$Model->alias]['count'] < $this->settings[$Model->alias]['limit'] ) | |
{ | |
$this->settings[$Model->alias]['limit'] = $this->settings[$Model->alias]['count']; | |
} | |
$this->model = $Model; | |
} | |
function random( $model, $type = 'list', $limit = null ) | |
{ | |
if ( ( ( $limit != null ) && ( (int)$limit > 0 ) ) && ( $this->settings[$this->model->alias]['count'] > $limit ) ) | |
{ | |
$this->settings[$this->model->alias]['limit'] = $limit; | |
} | |
$Model = ClassRegistry::init( $this->settings[$this->model->alias]['model'] ); | |
$ids = array(); | |
while( count( $ids ) < $this->settings[$this->model->alias]['limit'] ) | |
{ | |
srand ( (double) microtime( ) * 1000000 ); | |
$limit = rand( 0, $this->settings[$this->model->alias]['count'] ); | |
$_id = $Model->find( | |
'list', | |
array( | |
'conditions' => $this->settings[$this->model->alias]['conditions'], | |
'fields' => array( | |
$this->settings[$this->model->alias]['model'].'.'.$this->settings[$this->model->alias]['primaryKey'], | |
$this->settings[$this->model->alias]['model'].'.'.$this->settings[$this->model->alias]['primaryKey'] ), | |
'limit' => $limit.',1' | |
) | |
); | |
if ( !in_array( $_id, $ids ) ) | |
{ | |
$ids = $ids + $_id; | |
} | |
} // while | |
switch( $type ) | |
{ | |
case 'array': | |
return $ids; | |
break; | |
default: | |
return implode( ',', $ids ); | |
break; | |
} // switch | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment