This commit is contained in:
2020-09-08 01:13:09 +08:00
parent 0cbf083621
commit 3680ab1dfb
2 changed files with 22 additions and 3 deletions
+5 -1
View File
@@ -9,8 +9,11 @@
namespace Database\Base; namespace Database\Base;
use ArrayIterator;
use Exception;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Database\ActiveRecord; use Database\ActiveRecord;
use Traversable;
/** /**
* Class AbstractCollection * Class AbstractCollection
@@ -79,7 +82,8 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
} }
/** /**
* @return \ArrayIterator|\Traversable * @return ArrayIterator|Traversable
* @throws Exception
*/ */
public function getIterator() public function getIterator()
{ {
+17 -2
View File
@@ -5,6 +5,7 @@ namespace Database\Base;
use Database\ActiveRecord; use Database\ActiveRecord;
use ReflectionException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -25,7 +26,7 @@ class CollectionIterator extends \ArrayIterator
* @param $model * @param $model
* @param array $array * @param array $array
* @param int $flags * @param int $flags
* @throws \ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function __construct($model, $array = array(), $flags = 0) 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 * @return ActiveRecord|mixed
*/ */
public function current() public function current()
{ {
return (clone $this->model)->setAttributes(parent::current()); $current = parent::current();
if ($current instanceof ActiveRecord) {
return $current;
}
return $this->newModel($current);
} }