This commit is contained in:
2023-12-18 17:59:42 +08:00
parent 8c3cc60cc3
commit 4bcd4a8a44
2 changed files with 50 additions and 54 deletions
+25 -28
View File
@@ -231,11 +231,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param int|string|array $param * @param int|string|array $param
* @param null $db
* @return Model|null * @return Model|null
* @throws * @throws
*/ */
public static function findOne(int|string|array $param, $db = NULL): ?static public static function findOne(int|string|array $param): ?static
{ {
$model = static::instance(); $model = static::instance();
@@ -258,11 +257,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param int $param * @param int $param
* @param null $db
* @return Model|null * @return Model|null
* @throws * @throws
*/ */
public static function primary(int $param, $db = NULL): ?static public static function primary(int $param): ?static
{ {
$model = static::instance(); $model = static::instance();
$query = new ActiveQuery($model); $query = new ActiveQuery($model);
@@ -523,10 +521,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $value * @param array $value
* @return $this * @return $this
*/ */
public function populates($value): static public function populates(array $value): static
{ {
$this->_attributes = $value; $this->_attributes = $value;
$this->_oldAttributes = $value; $this->_oldAttributes = $value;
@@ -555,11 +553,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $rule * @param array $rule
* @return Validator * @return Validator
* @throws * @throws
*/ */
private function resolve($rule): Validator private function resolve(array $rule): Validator
{ {
$validate = new Validator(); $validate = new Validator();
foreach ($rule as $val) { foreach ($rule as $val) {
@@ -596,11 +594,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $attribute * @param string $attribute
* @return bool * @return bool
* @throws * @throws
*/ */
public function has($attribute): bool public function has(string $attribute): bool
{ {
return true; return true;
} }
@@ -626,11 +624,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $oldAttributes * @param array $oldAttributes
* @param $changeAttributes * @param array $changeAttributes
* @return bool * @return bool
*/ */
public function afterSave($oldAttributes, $changeAttributes): bool public function afterSave(array $oldAttributes, array $changeAttributes): bool
{ {
return TRUE; return TRUE;
} }
@@ -656,12 +654,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
return $this; return $this;
} }
/** /**
* @param $name * @param string $name
* @param $value * @param mixed $value
* @throws * @return void
*/ */
public function __set($name, $value): void public function __set(string $name, mixed $value): void
{ {
if ($this->hasRelateMethod($name, 'set')) { if ($this->hasRelateMethod($name, 'set')) {
$this->{'set' . ucfirst($name)}($value); $this->{'set' . ucfirst($name)}($value);
@@ -676,11 +675,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $name * @param string $name
* @return mixed * @return mixed
* @throws
*/ */
public function __get($name): mixed public function __get(string $name): mixed
{ {
$value = $this->_attributes[$name] ?? null; $value = $this->_attributes[$name] ?? null;
if (!$this->hasRelateMethod($name)) { if (!$this->hasRelateMethod($name)) {
@@ -692,12 +690,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $name * @param string $name
* @param null $value * @param mixed|null $value
* @return mixed * @return mixed
* @throws
*/ */
protected function withPropertyOverride($name, $value = null): mixed protected function withPropertyOverride(string $name, mixed $value = null): mixed
{ {
$method = 'get' . ucfirst($name) . 'Attribute'; $method = 'get' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
@@ -720,10 +717,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $name * @param string $name
* @return mixed|null * @return mixed
*/ */
protected function withRelate($name): mixed protected function withRelate(string $name): mixed
{ {
$response = $this->{'get' . ucfirst($name)}(); $response = $this->{'get' . ucfirst($name)}();
if ($response instanceof \Database\Traits\Relation) { if ($response instanceof \Database\Traits\Relation) {
@@ -737,7 +734,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param $name * @param $name
* @return bool * @return bool
*/ */
public function __isset($name): bool public function __isset(string $name): bool
{ {
return isset($this->_attributes[$name]); return isset($this->_attributes[$name]);
} }
+25 -26
View File
@@ -168,12 +168,11 @@ class Model extends Base\Model
/** /**
* @param $columns * @param array $columns
* @param $action * @param string $action
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws
*/ */
private function mathematics($columns, $action): int|bool|array|string|null private function mathematics(array $columns, string $action): int|bool|array|string|null
{ {
$condition = [$this->getPrimary() => $this->getPrimaryValue()]; $condition = [$this->getPrimary() => $this->getPrimaryValue()];
$activeQuery = static::query()->where($condition); $activeQuery = static::query()->where($condition);
@@ -249,10 +248,10 @@ class Model extends Base\Model
/** /**
* @param $data * @param array $data
* @return array * @return array
*/ */
private function withs($data): array private function withs(array $data): array
{ {
$with = $this->getWith(); $with = $this->getWith();
foreach ($with as $value) { foreach ($with as $value) {
@@ -268,12 +267,12 @@ class Model extends Base\Model
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param string $foreignKey
* @param $localKey * @param string $localKey
* @return string * @return string
* @throws * @throws Exception
*/ */
private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey): string private function _hasBase(ModelInterface|string $modelName, string $foreignKey, string $localKey): string
{ {
if (($value = $this->{$localKey}) === null) { if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");
@@ -291,12 +290,12 @@ class Model extends Base\Model
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param string $foreignKey
* @param $localKey * @param string $localKey
* @return HasOne|ActiveQuery * @return HasOne|ActiveQuery
* @throws * @throws Exception
*/ */
public function hasOne(ModelInterface|string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery public function hasOne(ModelInterface|string $modelName, string $foreignKey, string $localKey): HasOne|ActiveQuery
{ {
return new HasOne($this->_hasBase($modelName, $foreignKey, $localKey)); return new HasOne($this->_hasBase($modelName, $foreignKey, $localKey));
} }
@@ -304,12 +303,12 @@ class Model extends Base\Model
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param string $foreignKey
* @param $localKey * @param string $localKey
* @return ActiveQuery|HasCount * @return ActiveQuery|HasCount
* @throws * @throws Exception
*/ */
public function hasCount(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasCount public function hasCount(ModelInterface|string $modelName, string $foreignKey, string $localKey): ActiveQuery|HasCount
{ {
return new HasCount($this->_hasBase($modelName, $foreignKey, $localKey)); return new HasCount($this->_hasBase($modelName, $foreignKey, $localKey));
} }
@@ -317,24 +316,24 @@ class Model extends Base\Model
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param string $foreignKey
* @param $localKey * @param string $localKey
* @return ActiveQuery|HasMany * @return ActiveQuery|HasMany
* @throws * @throws Exception
*/ */
public function hasMany(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany public function hasMany(ModelInterface|string $modelName, string $foreignKey, string $localKey): ActiveQuery|HasMany
{ {
return new HasMany($this->_hasBase($modelName, $foreignKey, $localKey)); return new HasMany($this->_hasBase($modelName, $foreignKey, $localKey));
} }
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param string $foreignKey
* @param $localKey * @param string $localKey
* @return ActiveQuery|HasMany * @return ActiveQuery|HasMany
* @throws * @throws Exception
*/ */
public function hasIn(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany public function hasIn(ModelInterface|string $modelName, string $foreignKey, string $localKey): ActiveQuery|HasMany
{ {
if (($value = $this->{$localKey}) === null) { if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");