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