*
* This will loop over all users, but will retrieve them 100 by 100:
* <code>
- * foreach( new Iterators\Query( array( 'query' => 'SELECT * FROM users', 'limit' => 100 ) ) as $user ) {
+ * foreach( new Iterators\Query( 'SELECT * FROM users', 100 ) as $user ) {
* tickle( $user );
* }
* </code>
*
- *
- * @param array $args Supported arguments:
- * query – the query as a string. It shouldn't include any LIMIT clauses
- * limit – (optional) how many rows to retrieve at once, default value is 500
+ * @param string $query The query as a string. It shouldn't include any LIMIT clauses
+ * @param number $limit How many rows to retrieve at once; default value is 500 (optional)
*/
- function __construct( $args = array() ) {
+ public function __construct( $query, $limit = 500 ) {
+ $this->query = $query;
+ $this->limit = $limit;
+
$this->db = $GLOBALS['wpdb'];
- foreach( $args as $key => $value ) {
- $this->$key = $value;
- }
- if ( !$this->query ) {
- throw new InvalidArgumentException( 'Missing query argument.' );
- }
}
function load_items_from_db() {
$conditions = self::build_where_conditions( $args['where'] );
$where_sql = $conditions? " WHERE $conditions" : '';
$query = "SELECT $fields FROM $table $where_sql";
- $parent_args = compact( 'query' );
- if ( isset( $args['limit'] ) ) {
- $parent_args['limit'] = $args['limit'];
- }
- parent::__construct( $parent_args );
+
+ $limit = isset( $args['limit'] ) ? $args['limit'] : 500;
+
+ parent::__construct( $query, $limit );
}
static function build_fields( $fields ) {