From 3680ab1dfbb0da32572b9984422457987a6ece34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 8 Sep 2020 01:13:09 +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/Base/AbstractCollection.php | 6 +++++- Database/Base/CollectionIterator.php | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Database/Base/AbstractCollection.php b/Database/Base/AbstractCollection.php index cac9d7b0..34edb3c1 100644 --- a/Database/Base/AbstractCollection.php +++ b/Database/Base/AbstractCollection.php @@ -9,8 +9,11 @@ namespace Database\Base; +use ArrayIterator; +use Exception; use Snowflake\Abstracts\Component; use Database\ActiveRecord; +use Traversable; /** * Class AbstractCollection @@ -79,7 +82,8 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat } /** - * @return \ArrayIterator|\Traversable + * @return ArrayIterator|Traversable + * @throws Exception */ public function getIterator() { diff --git a/Database/Base/CollectionIterator.php b/Database/Base/CollectionIterator.php index 2e7e873d..c877b55b 100644 --- a/Database/Base/CollectionIterator.php +++ b/Database/Base/CollectionIterator.php @@ -5,6 +5,7 @@ namespace Database\Base; use Database\ActiveRecord; +use ReflectionException; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -25,7 +26,7 @@ class CollectionIterator extends \ArrayIterator * @param $model * @param array $array * @param int $flags - * @throws \ReflectionException + * @throws ReflectionException * @throws NotFindClassException */ public function __construct($model, $array = array(), $flags = 0) @@ -38,12 +39,26 @@ class CollectionIterator extends \ArrayIterator } + /** + * @param $current + * @return ActiveRecord + */ + protected function newModel($current) + { + return (clone $this->model)->setAttributes($current); + } + + /** * @return ActiveRecord|mixed */ public function current() { - return (clone $this->model)->setAttributes(parent::current()); + $current = parent::current(); + if ($current instanceof ActiveRecord) { + return $current; + } + return $this->newModel($current); }