This commit is contained in:
2020-09-08 01:21:07 +08:00
parent 51b79ed1ea
commit 17093acb9f
2 changed files with 24 additions and 7 deletions
+11 -2
View File
@@ -234,9 +234,18 @@ class ActiveQuery extends Component
* @return ActiveRecord * @return ActiveRecord
* @throws Exception * @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)) { if (empty($this->with) || !is_array($this->with)) {
return $model; return $model;
} }
+13 -5
View File
@@ -4,6 +4,7 @@
namespace Database\Base; namespace Database\Base;
use Database\ActiveQuery;
use Database\ActiveRecord; use Database\ActiveRecord;
use ReflectionException; use ReflectionException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
@@ -21,20 +22,26 @@ class CollectionIterator extends \ArrayIterator
private $model; private $model;
/** @var ActiveQuery */
private $query;
/** /**
* CollectionIterator constructor. * CollectionIterator constructor.
* @param $model * @param $model
* @param $query
* @param array $array * @param array $array
* @param int $flags * @param int $flags
* @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException
*/ */
public function __construct($model, $array = array(), $flags = 0) public function __construct($model, $query, $array = array(), $flags = 0)
{ {
$this->model = $model; $this->model = $model;
if (is_string($model)) { if (is_string($model)) {
$this->model = Snowflake::createObject($model); $this->model = Snowflake::createObject($model);
} }
$this->query = $query;
parent::__construct($array, $flags); parent::__construct($array, $flags);
} }
@@ -46,6 +53,7 @@ class CollectionIterator extends \ArrayIterator
protected function newModel($current) protected function newModel($current)
{ {
return (clone $this->model)->setAttributes($current); return (clone $this->model)->setAttributes($current);
} }
@@ -55,10 +63,10 @@ class CollectionIterator extends \ArrayIterator
public function current() public function current()
{ {
$current = parent::current(); $current = parent::current();
if ($current instanceof ActiveRecord) { if (!($current instanceof ActiveRecord)) {
return $current; $current = $this->newModel($current);
} }
return $this->newModel($current); return $this->query->getWith($current);
} }