This commit is contained in:
2021-08-05 17:55:15 +08:00
parent 73c148e1a1
commit 1d585313bc
2 changed files with 44 additions and 18 deletions
+8
View File
@@ -92,6 +92,14 @@ class Annotation extends Component
} }
public function runGet($name, $value)
{
}
/** /**
* @param string $class * @param string $class
* @param string|null $method * @param string|null $method
+36 -18
View File
@@ -15,8 +15,6 @@ use Annotation\Inject;
use ArrayAccess; use ArrayAccess;
use Database\ActiveQuery; use Database\ActiveQuery;
use Database\ActiveRecord; use Database\ActiveRecord;
use Database\Affair\BeginTransaction;
use Database\Affair\Rollback;
use Database\Connection; use Database\Connection;
use Database\HasMany; use Database\HasMany;
use Database\HasOne; use Database\HasOne;
@@ -47,6 +45,7 @@ use validator\Validator;
* @method rules() * @method rules()
* @method static tableName() * @method static tableName()
* @property Application $container * @property Application $container
* @property EventDispatch $eventDispatch
*/ */
abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
{ {
@@ -113,22 +112,23 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
/** /**
* @return Application * @return Application
*/ */
#[Pure] private function getContainer(): Application #[Pure] protected function getContainer(): Application
{ {
return Snowflake::app(); return Snowflake::app();
} }
/** /**
* @param EventDispatch $eventDispatch * @return EventDispatch
* @param array $config * @throws NotFindClassException
* @throws Exception * @throws ReflectionException
*/ */
public function __construct(public EventDispatch $eventDispatch, array $config = []) protected function getEventDispatch(): EventDispatch
{ {
parent::__construct($config); return Snowflake::getDi()->get(EventDispatch::class);
} }
/** /**
* @param $data * @param $data
* @return ActiveRecord * @return ActiveRecord
@@ -756,7 +756,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
* @param $model * @param $model
* @return bool * @return bool
*/ */
#[Event(ActiveRecord::BEFORE_SAVE)] #[Event(self::BEFORE_SAVE)]
public function beforeSave($model): bool public function beforeSave($model): bool
{ {
return true; return true;
@@ -829,28 +829,46 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
public function __get($name): mixed public function __get($name): mixed
{ {
$value = $this->_attributes[$name] ?? null; $method = 'get' . ucfirst($name);
$method = $this->_get_annotation($name, static::GET); if (!method_exists($this, $method)) {
if (!empty($method)) { return $this->{$method}();
return $this->{$method}($value); }
if (isset($this->_attributes[$name])) {
return $this->runGetter($name, $this->_attributes[$name]);
} else {
return $this->runRelation($name);
} }
return $this->runRelation($name, $value);
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
* @return mixed * @return void|null
* @throws Exception * @throws Exception
*/ */
private function runRelation($name, $value): mixed private function runGetter($name, $value)
{ {
$relation = $this->_get_annotation($name, static::RELATE); $relation = $this->_get_annotation($name, static::GET);
if (empty($relation)) { if (empty($relation)) {
return $this->_decode($name, $value); return $this->_decode($name, $value);
} }
if (($value = $this->{$relation}(...[$value])) instanceof HasBase) { return $this->{$relation}($value);
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
private function runRelation($name): mixed
{
$relation = $this->_get_annotation($name, static::RELATE);
if (empty($relation)) {
return null;
}
if (($value = $this->{$relation}()) instanceof HasBase) {
return $value->get(); return $value->get();
} }
return $value; return $value;