This commit is contained in:
2021-04-25 17:01:57 +08:00
parent 4b0e457318
commit c17647859a
5 changed files with 204 additions and 163 deletions
+3 -4
View File
@@ -115,11 +115,10 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
*/
public function getModel(): ActiveRecord
{
$model = $this->model;;
if (is_object($model)) {
return $model;
if (!is_object($this->model)) {
$this->model = $this->model::populate([]);
}
return $model::populate([]);
return $this->model;
}
+26 -4
View File
@@ -139,6 +139,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} else {
$this->_relation = Context::getContext(Relation::class);
}
$annotation = Snowflake::app()->getAnnotation();
$this->_annotations[self::ANNOTATION_GET] = $annotation->getGets(static::class);
$this->_annotations[self::ANNOTATION_SET] = $annotation->getSets(static::class);
$this->_relate = $annotation->getRelateMethods(static::class);
}
@@ -809,7 +814,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
parent::__set($name, $value);
return;
}
$method = annotation()->getSetMethodName(static::class, $name);
$method = $this->_get_annotation($name, self::ANNOTATION_SET);
if (!empty($method)) {
$value = $this->{$method}($value);
}
@@ -817,6 +822,22 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
}
/**
* @param $name
* @param string $method
* @return string|null
*/
protected function _get_annotation(string $name = null, string $method = self::ANNOTATION_GET): ?string
{
$matches = match ($method) {
self::ANNOTATION_GET => $this->_annotations[self::ANNOTATION_GET],
self::ANNOTATION_SET => $this->_annotations[self::ANNOTATION_SET],
default => $this->_relate
};
return $matches[$name] ?? null;
}
/**
* @param $name
* @return mixed
@@ -840,9 +861,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
private function _gets(string $name, mixed $value): mixed
{
$loader = Snowflake::app()->getAnnotation();
$method = $loader->getGetMethodName(static::class, $name);
if (empty($method)) {
if (!empty($method = $this->_get_annotation($name))) {
return $this->{$method}(...[$value]);
}
if (empty($method = $this->_get_annotation())) {
return $this->_decode($name, $value);
}
$_value = $this->{$method}(...[$value]);
+7 -5
View File
@@ -8,10 +8,6 @@ namespace Database\Base;
use Database\ActiveQuery;
use Database\ActiveRecord;
use Exception;
use ReflectionException;
use Snowflake\Channel;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
@@ -28,6 +24,9 @@ class CollectionIterator extends \ArrayIterator
private ActiveQuery $query;
private ?ActiveRecord $_clone = null;
public function clean()
{
unset($this->query);
@@ -57,7 +56,10 @@ class CollectionIterator extends \ArrayIterator
*/
protected function newModel($current): ActiveRecord
{
return $this->model::populate($current);
$model = clone $this->model;
$model->setAttributes($current);
$model->refresh();
return $model;
}