Skip to content

Instantly share code, notes, and snippets.

@thinkt4nk
Created August 10, 2011 18:37
Show Gist options
  • Save thinkt4nk/1137736 to your computer and use it in GitHub Desktop.
Save thinkt4nk/1137736 to your computer and use it in GitHub Desktop.
POC for a iterative model retrieval method for Yii
<?php
class EIterableActiveRecord extends CActiveRecord
{
public function getIter($buffer_size)
{
return new ActiveRecordIterator($this,$buffer_size);
}
}
class ActiveRecordIterator implements Iterator
{
protected $model;
protected $index;
protected $buffer_size;
public function __construct($model,$buffer_size)
{
$this->model = $model;
$this->buffer_size = $buffer_size;
$this->rewind();
}
public function rewind()
{
$this->index = 0;
}
public function current()
{
$criteria = $this->getCriteria($this->index);
return $this->model->findAll($criteria);
}
private function getCriteria($index)
{
return new CDbCriteria(array(
'limit' => $this->buffer_size,
'offset' => ($this->buffer_size * $index),
));
}
public function key()
{
return $this->index;
}
public function next()
{
++$this->index;
}
public function valid()
{
$models = $this->model->findAll($this->getCriteria($this->index));
return !empty($models);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment