diff --git a/Database/ActiveQuery.php b/Database/ActiveQuery.php index 97ce555d..92ba42b2 100644 --- a/Database/ActiveQuery.php +++ b/Database/ActiveQuery.php @@ -219,21 +219,12 @@ class ActiveQuery extends Component public function all() { $data = $this->modelClass::getDb() - ->createCommand($this->getBuild()->count($this)) + ->createCommand($this->queryBuilder()) ->all(); $collect = new Collection(); $collect->setModel($this->modelClass); - if (empty($data) || !is_array($data)) { - return $this->asArray ? [] : $collect; - } - - $_tmp = []; - $model = Snowflake::createObject($this->modelClass); - foreach ($data as $key => $val) { - $_tmp[] = $this->populate(clone $model, $val); - } - $collect->setItems($_tmp); + $collect->setItems($data); if ($this->asArray) { return $collect->toArray(); } diff --git a/Database/Base/AbstractCollection.php b/Database/Base/AbstractCollection.php index 6408c011..d5f44c36 100644 --- a/Database/Base/AbstractCollection.php +++ b/Database/Base/AbstractCollection.php @@ -79,7 +79,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat */ public function getIterator() { - return new \ArrayIterator($this->_item); + return new CollectionIterator($this->model, $this->_item); } /** diff --git a/Database/Base/CollectionIterator.php b/Database/Base/CollectionIterator.php new file mode 100644 index 00000000..40005ba5 --- /dev/null +++ b/Database/Base/CollectionIterator.php @@ -0,0 +1,37 @@ +model = $model; + parent::__construct($array, $flags); + } + + + /** + * @return ActiveRecord|mixed + */ + public function current() + { + return $this->model::populate(parent::current()); + } + + +}