This commit is contained in:
2021-04-25 11:27:13 +08:00
parent 77a6445eea
commit be8faa5cb0
6 changed files with 374 additions and 371 deletions
+6 -4
View File
@@ -13,6 +13,7 @@ namespace Database;
use Database\Base\BaseActiveRecord;
use Database\Traits\HasBase;
use Exception;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Channel;
use Snowflake\Exception\NotFindClassException;
@@ -167,8 +168,9 @@ class ActiveRecord extends BaseActiveRecord
* @return static
* @throws Exception
*/
private static function getModelClass(): static
#[Pure] private static function getModelClass(): static
{
return new static();
$className = get_called_class();
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
@@ -305,7 +307,7 @@ class ActiveRecord extends BaseActiveRecord
{
$data = $this->_attributes;
$lists = Snowflake::getAnnotation()->getModelMethods(get_called_class());
$lists = Snowflake::getAnnotation()->getModelMethods(static::class);
foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null);
}
@@ -334,10 +336,10 @@ class ActiveRecord extends BaseActiveRecord
* @param string $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery
* @return HasOne|ActiveQuery
* @throws Exception
*/
public function hasOne(string $modelName, $foreignKey, $localKey): mixed
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
+5 -4
View File
@@ -350,7 +350,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
public static function find(): ActiveQuery
{
return Snowflake::createObject(ActiveQuery::class, [get_called_class()]);
return Snowflake::createObject(ActiveQuery::class, [static::class]);
}
/**
@@ -728,11 +728,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function getRelate($name): mixed
{
if (empty($this->_relate[$name])) {
$this->_relate = Snowflake::getAnnotation()->getRelateMethods(get_called_class());
$this->_relate = Snowflake::getAnnotation()->getRelateMethods(static::class);
}
return $this->_relate[$name] ?? null;
}
@@ -827,7 +828,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
parent::__set($name, $value);
return;
}
$method = annotation()->getSetMethodName(get_called_class(), $name);
$method = annotation()->getSetMethodName(static::class, $name);
if (!empty($method)) {
$value = $this->{$method}($value);
}
@@ -845,7 +846,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
$value = $this->_attributes[$name] ?? null;
$loader = Snowflake::app()->getAnnotation();
if (!empty($method = $loader->getGetMethodName(get_called_class(), $name))) {
if (!empty($method = $loader->getGetMethodName(static::class, $name))) {
return $this->{$method}(...[$value]);
}
if (array_key_exists($name, $this->_attributes)) {
+1 -1
View File
@@ -46,7 +46,7 @@ abstract class HttpService extends Component
if (property_exists($this, $name)) {
return $this->$name;
}
$message = sprintf('method %s::%s not exists.', get_called_class(), $name);
$message = sprintf('method %s::%s not exists.', static::class, $name);
throw new Exception($message);
}
+2 -2
View File
@@ -65,7 +65,7 @@ class BaseObject implements Configure
*/
#[Pure] public static function className(): string
{
return get_called_class();
return static::class;
}
/**
@@ -80,7 +80,7 @@ class BaseObject implements Configure
if (method_exists($this, $method)) {
$this->{$method}($value);
} else {
$this->error('set ' . $name . ' not exists ' . get_called_class());
$this->error('set ' . $name . ' not exists ' . static::class);
throw new Exception('The set name ' . $name . ' not find in class ' . static::class);
}
}
+1 -1
View File
@@ -60,7 +60,7 @@ abstract class Process extends \Swoole\Process implements SProcess
*/
#[Pure] private function getPrefix(): string
{
return get_called_class();
return static::class;
}
+1 -1
View File
@@ -128,7 +128,7 @@ abstract class BaseValidator
} else if (property_exists($this, $name)) {
$this->$name = $value;
} else {
throw new Exception('unknown property ' . $name . ' in class ' . get_called_class());
throw new Exception('unknown property ' . $name . ' in class ' . static::class);
}
}
}