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
+19 -1
View File
@@ -56,6 +56,25 @@ class Annotation extends Component
} }
/**
* @param $class
* @return array
*/
public function getGets($class): array
{
return $this->_model_gets[$class] ?? [];
}
/**
* @param $class
* @return array
*/
public function getSets($class): array
{
return $this->_model_gets[$class] ?? [];
}
/** /**
* @param string $class * @param string $class
* @param string $setName * @param string $setName
@@ -91,7 +110,6 @@ class Annotation extends Component
} }
/** /**
* @param string $class * @param string $class
* @param string $setName * @param string $setName
+1 -1
View File
@@ -307,7 +307,7 @@ class ActiveRecord extends BaseActiveRecord
{ {
$data = $this->_attributes; $data = $this->_attributes;
$lists = Snowflake::getAnnotation()->getModelMethods(static::class); $lists = $this->getAnnotation(self::ANNOTATION_GET);
foreach ($lists as $key => $item) { foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null); $data[$key] = $this->{$item}($data[$key] ?? null);
} }
+3 -4
View File
@@ -115,11 +115,10 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
*/ */
public function getModel(): ActiveRecord public function getModel(): ActiveRecord
{ {
$model = $this->model;; if (!is_object($this->model)) {
if (is_object($model)) { $this->model = $this->model::populate([]);
return $model;
} }
return $model::populate([]); return $this->model;
} }
+26 -4
View File
@@ -139,6 +139,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} else { } else {
$this->_relation = Context::getContext(Relation::class); $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); parent::__set($name, $value);
return; return;
} }
$method = annotation()->getSetMethodName(static::class, $name); $method = $this->_get_annotation($name, self::ANNOTATION_SET);
if (!empty($method)) { if (!empty($method)) {
$value = $this->{$method}($value); $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 * @param $name
* @return mixed * @return mixed
@@ -840,9 +861,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
private function _gets(string $name, mixed $value): mixed private function _gets(string $name, mixed $value): mixed
{ {
$loader = Snowflake::app()->getAnnotation(); if (!empty($method = $this->_get_annotation($name))) {
$method = $loader->getGetMethodName(static::class, $name); return $this->{$method}(...[$value]);
if (empty($method)) { }
if (empty($method = $this->_get_annotation())) {
return $this->_decode($name, $value); return $this->_decode($name, $value);
} }
$_value = $this->{$method}(...[$value]); $_value = $this->{$method}(...[$value]);
+7 -5
View File
@@ -8,10 +8,6 @@ namespace Database\Base;
use Database\ActiveQuery; use Database\ActiveQuery;
use Database\ActiveRecord; use Database\ActiveRecord;
use Exception; 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 ActiveQuery $query;
private ?ActiveRecord $_clone = null;
public function clean() public function clean()
{ {
unset($this->query); unset($this->query);
@@ -57,7 +56,10 @@ class CollectionIterator extends \ArrayIterator
*/ */
protected function newModel($current): ActiveRecord protected function newModel($current): ActiveRecord
{ {
return $this->model::populate($current); $model = clone $this->model;
$model->setAttributes($current);
$model->refresh();
return $model;
} }