This commit is contained in:
2024-12-20 18:20:54 +08:00
parent 654cc483b3
commit 7b4604ce98
+63 -21
View File
@@ -63,7 +63,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @var bool
*/
protected bool $skipValidate = false;
protected bool $skipValidate = FALSE;
/**
@@ -106,6 +106,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $data
*
* @return Model
*/
public function setWith(array $data): static
@@ -164,6 +165,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param bool $bool
*
* @return $this
*/
public function setIsNowExample(bool $bool = FALSE): static
@@ -212,9 +214,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
public function hasPrimaryValue(): bool
{
if ($this->hasPrimary()) {
return $this->getPrimaryValue() === null;
return $this->getPrimaryValue() === NULL;
}
return false;
return FALSE;
}
@@ -227,19 +229,20 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
if ($this->hasPrimary() && isset($this->_oldAttributes[$this->getPrimary()])) {
return (int)$this->_oldAttributes[$this->getPrimary()];
} else {
return null;
return NULL;
}
}
/**
* @param int|string|array $param
*
* @return Model|null
* @throws
*/
public static function findOne(int|string|array|null $param): ?static
{
if (empty($param)) {
return null;
return NULL;
}
$query = new ActiveQuery($model = static::instance());
$query->from($model->getTable())->alias('t1');
@@ -250,7 +253,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
} else {
$query->whereRaw($param);
}
if (($data = $query->first()) === false) {
if (($data = $query->first()) === FALSE) {
throw new Exception($model->getLastError());
} else {
return $data;
@@ -260,6 +263,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param int $param
*
* @return Model|null
* @throws
*/
@@ -293,6 +297,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param int|string|array $condition
*
* @return static|null
* @throws
*/
@@ -304,6 +309,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string|array $condition
*
* @return Collection
* @throws
*/
@@ -381,6 +387,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
* @param mixed $value
*
* @return mixed
*/
public function setAttribute(string $name, mixed $value): mixed
@@ -395,6 +402,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
* @param mixed $value
*
* @return mixed
*/
public function setOldAttribute(string $name, mixed $value): mixed
@@ -408,6 +416,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $param
*
* @return $this
* @throws
*/
@@ -422,6 +431,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $param
*
* @return $this
*/
public function setOldAttributes(array $param): static
@@ -441,8 +451,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
{
$sql = SqlBuilder::builder($query = static::query())->insert($this->_attributes);
$lastId = $this->getConnection()->createCommand($sql, $query->params)->exec();
if ($lastId === false) {
return false;
if ($lastId === FALSE) {
return FALSE;
}
if (!$this->hasPrimary()) {
return $this->refresh()->afterSave($this->_attributes, []);
@@ -458,6 +468,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param array $old
* @param array $condition
* @param array $change
*
* @return $this|bool
* @throws
*/
@@ -465,11 +476,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
{
$query = static::query()->where($condition);
if (count($change) < 1) {
return true;
return TRUE;
}
$generate = SqlBuilder::builder($query)->update($change);
if ($generate === false) {
return false;
if ($generate === FALSE) {
return FALSE;
}
if (!$this->getConnection()->createCommand($generate, $query->params)->exec()) {
return FALSE;
@@ -531,6 +542,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $value
*
* @return $this
*/
public function populates(array $value): static
@@ -544,6 +556,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $rule
*
* @return bool
* @throws
*/
@@ -563,6 +576,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $rule
*
* @return Validator
* @throws
*/
@@ -583,6 +597,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
*
* @return null
* @throws
*/
@@ -604,12 +619,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $attribute
*
* @return bool
* @throws
*/
public function has(string $attribute): bool
{
return true;
return TRUE;
}
@@ -635,6 +651,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $oldAttributes
* @param array $changeAttributes
*
* @return bool
*/
public function afterSave(array $oldAttributes, array $changeAttributes): bool
@@ -645,6 +662,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param self $model
*
* @return bool
* @throws
*/
@@ -660,7 +678,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
public function refresh(): static
{
$this->_oldAttributes = $this->_attributes;
$this->isNewExample = false;
$this->isNewExample = FALSE;
return $this;
}
@@ -668,6 +686,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set(string $name, mixed $value): void
@@ -686,11 +705,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
*
* @return mixed
*/
public function __get(string $name): mixed
{
$value = $this->_attributes[$name] ?? null;
$value = $this->_attributes[$name] ?? NULL;
if (!$this->hasRelateMethod($name)) {
return $this->withPropertyOverride($name, $value);
} else {
@@ -702,9 +722,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
* @param mixed|null $value
*
* @return mixed
*/
protected function withPropertyOverride(string $name, mixed $value = null): mixed
protected function withPropertyOverride(string $name, mixed $value = NULL): mixed
{
$method = 'get' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
@@ -718,21 +739,31 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
* @param string $prefix
*
* @return bool
*/
protected function hasRelateMethod(string $name, string $prefix = 'get'): bool
protected function hasRelateMethod(string $name): bool
{
return method_exists($this, $prefix . ucfirst($name));
$reflection = $this->container->get(static::class);
if (!$reflection->hasMethod($name)) {
return FALSE;
}
if ($reflection->getMethod($name)->isStatic() || !$reflection->getMethod($name)->isPublic()) {
return FALSE;
} else {
return TRUE;
}
}
/**
* @param string $name
*
* @return mixed
*/
protected function withRelate(string $name): mixed
{
$response = $this->{'get' . ucfirst($name)}();
$response = $this->$name();
if ($response instanceof \Database\Traits\Relation) {
$response = $response->get();
}
@@ -742,6 +773,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param $name
*
* @return bool
*/
public function __isset(string $name): bool
@@ -752,6 +784,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param mixed $offset
*
* @return bool
* @throws
*/
@@ -762,6 +795,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param mixed $offset
*
* @return mixed
* @throws
*/
@@ -773,18 +807,22 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param mixed $offset
* @param mixed $value
*
* @throws
*/
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value): void
#[ReturnTypeWillChange]
public function offsetSet(mixed $offset, mixed $value): void
{
$this->__set($offset, $value);
}
/**
* @param mixed $offset
*
* @throws
*/
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset): void
#[ReturnTypeWillChange]
public function offsetUnset(mixed $offset): void
{
if (!isset($this->_attributes[$offset]) && !isset($this->_oldAttributes[$offset])) {
return;
@@ -795,6 +833,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string ...$params
*
* @return array
*/
public function unset(string ...$params): array
@@ -805,6 +844,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $data
*
* @return static
* @throws
*/
@@ -821,6 +861,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public static function __callStatic(string $name, array $arguments)
@@ -831,11 +872,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $field
*
* @return array
*/
public function getOldAttribute(string $field): mixed
{
return $this->_oldAttributes[$field] ?? null;
return $this->_oldAttributes[$field] ?? NULL;
}
}