From 6bab3c8f1e07f638d57ecf8b275e35513a0e0171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 8 Sep 2020 00:49:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/ActiveQuery.php | 13 ++-------- Database/Base/AbstractCollection.php | 2 +- Database/Base/CollectionIterator.php | 37 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 Database/Base/CollectionIterator.php 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()); + } + + +}