This commit is contained in:
as2252258@163.com
2021-04-24 19:46:24 +08:00
parent 7b8c0dbf63
commit 1867083b14
3 changed files with 385 additions and 376 deletions
+26 -11
View File
@@ -31,7 +31,7 @@ class Annotation extends Component
*/ */
public function addSets(string $class, string $setName, string $method) public function addSets(string $class, string $setName, string $method)
{ {
$this->_model_sets[$class . '::' . $setName] = $method; $this->_model_sets[$class][$setName] = $method;
} }
/** /**
@@ -41,7 +41,7 @@ class Annotation extends Component
*/ */
public function addGets(string $class, string $setName, string $method) public function addGets(string $class, string $setName, string $method)
{ {
$this->_model_gets[$class . '::' . $setName] = $method; $this->_model_gets[$class][$setName] = $method;
} }
@@ -52,7 +52,7 @@ class Annotation extends Component
*/ */
public function addRelate(string $class, string $setName, string $method) public function addRelate(string $class, string $setName, string $method)
{ {
$this->_model_relate[$class . '::' . $setName] = $method; $this->_model_relate[$class][$setName] = $method;
} }
@@ -63,14 +63,23 @@ class Annotation extends Component
*/ */
public function getGetMethodName(string $class, string $setName): ?string public function getGetMethodName(string $class, string $setName): ?string
{ {
if (isset($this->_model_gets[$class . '::' . $setName])) { $gets = $this->_model_gets[$class] ?? $this->_model_relate[$class] ?? null;
return $this->_model_gets[$class . '::' . $setName]; if ($gets == null) {
}
if (isset($this->_model_relate[$class . '::' . $setName])) {
return $this->_model_relate[$class . '::' . $setName];
}
return null; return null;
} }
return $gets[$setName] ?? null;
}
/**
* @param string $class
* @return array
*/
public function getModelMethods(string $class): array
{
return $this->_model_gets[$class] ?? [];
}
/** /**
@@ -80,8 +89,14 @@ class Annotation extends Component
*/ */
public function getSetMethodName(string $class, string $setName): ?string public function getSetMethodName(string $class, string $setName): ?string
{ {
if (isset($this->_model_sets[$class . '::' . $setName])) { if (!isset($this->_model_sets[$class])) {
return $this->_model_relate[$class . '::' . $setName]; return null;
}
$lists = $this->_model_sets[$class];
if (isset($lists[$setName])) {
return $lists[$setName];
} }
return null; return null;
} }
+3 -3
View File
@@ -304,10 +304,10 @@ class ActiveRecord extends BaseActiveRecord
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
foreach ($this->getAnnotation(self::ANNOTATION_GET) as $key => $item) {
if (!isset($data[$key])) continue;
$data[$key] = $this->runAnnotation($key, $data[$key] ?? null); $lists = Snowflake::getAnnotation()->getModelMethods(get_called_class());
foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null);
} }
$data = array_merge($data, $this->runRelate()); $data = array_merge($data, $this->runRelate());
return $data; return $data;
+1 -7
View File
@@ -383,13 +383,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
public function getAttributes(): array public function getAttributes(): array
{ {
$data = $this->_attributes; return $this->toArray();
foreach ($this->getAnnotation() as $key => $item) {
if (!isset($data[$key])) continue;
$data[$key] = $this->runAnnotation($key, $data[$key] ?? null);
}
return $data;
} }
/** /**