diff --git a/Database/ActiveQuery.php b/Database/ActiveQuery.php index 94125f39..9da036e2 100644 --- a/Database/ActiveQuery.php +++ b/Database/ActiveQuery.php @@ -234,9 +234,18 @@ class ActiveQuery extends Component * @return ActiveRecord * @throws Exception */ - private function populate($model, $data) + public function populate($model, $data) + { + return $this->getWith($model::populate($data)); + } + + + /** + * @param $model + * @return mixed + */ + public function getWith($model) { - $model = $model::populate($data); if (empty($this->with) || !is_array($this->with)) { return $model; } diff --git a/Database/Base/CollectionIterator.php b/Database/Base/CollectionIterator.php index c877b55b..d31fbeb0 100644 --- a/Database/Base/CollectionIterator.php +++ b/Database/Base/CollectionIterator.php @@ -4,6 +4,7 @@ namespace Database\Base; +use Database\ActiveQuery; use Database\ActiveRecord; use ReflectionException; use Snowflake\Exception\NotFindClassException; @@ -21,20 +22,26 @@ class CollectionIterator extends \ArrayIterator private $model; + /** @var ActiveQuery */ + private $query; + + /** * CollectionIterator constructor. * @param $model + * @param $query * @param array $array * @param int $flags - * @throws ReflectionException * @throws NotFindClassException + * @throws ReflectionException */ - public function __construct($model, $array = array(), $flags = 0) + public function __construct($model, $query, $array = array(), $flags = 0) { $this->model = $model; if (is_string($model)) { $this->model = Snowflake::createObject($model); } + $this->query = $query; parent::__construct($array, $flags); } @@ -46,6 +53,7 @@ class CollectionIterator extends \ArrayIterator protected function newModel($current) { return (clone $this->model)->setAttributes($current); + } @@ -55,10 +63,10 @@ class CollectionIterator extends \ArrayIterator public function current() { $current = parent::current(); - if ($current instanceof ActiveRecord) { - return $current; + if (!($current instanceof ActiveRecord)) { + $current = $this->newModel($current); } - return $this->newModel($current); + return $this->query->getWith($current); }