Created
August 10, 2011 18:37
-
-
Save thinkt4nk/1137736 to your computer and use it in GitHub Desktop.
POC for a iterative model retrieval method for Yii
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
<?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