This commit is contained in:
2021-12-12 04:29:35 +08:00
parent 76292522e4
commit e260e43c17
+27 -26
View File
@@ -263,13 +263,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
public function hasPrimary(): bool public function hasPrimary(): bool
{ {
if ($this->primary !== NULL) { if ($this->primary !== NULL) {
return true; return TRUE;
} }
$primary = $this->getColumns()->getPrimaryKeys(); $primary = $this->getColumns()->getPrimaryKeys();
if (!empty($primary)) { if (!empty($primary)) {
return $this->primary = is_array($primary) ? current($primary) : $primary; return $this->primary = is_array($primary) ? current($primary) : $primary;
} }
return false; return FALSE;
} }
@@ -278,7 +278,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function isAutoIncrement(): bool public function isAutoIncrement(): bool
{ {
return $this->getAutoIncrement() !== null; return $this->getAutoIncrement() !== NULL;
} }
/** /**
@@ -296,7 +296,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
public function getPrimary(): ?string public function getPrimary(): ?string
{ {
if (!$this->hasPrimary()) { if (!$this->hasPrimary()) {
return null; return NULL;
} }
return $this->primary; return $this->primary;
} }
@@ -308,7 +308,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
public function getPrimaryValue(): ?int public function getPrimaryValue(): ?int
{ {
if (!$this->hasPrimary()) { if (!$this->hasPrimary()) {
return null; return NULL;
} }
return $this->getAttribute($this->primary); return $this->getAttribute($this->primary);
} }
@@ -324,7 +324,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
public static function findOne($param, $db = NULL): static|null public static function findOne($param, $db = NULL): static|null
{ {
if (is_bool($param)) { if (is_bool($param)) {
return null; return NULL;
} }
if (is_numeric($param)) { if (is_numeric($param)) {
$param = static::getPrimaryCondition($param); $param = static::getPrimaryCondition($param);
@@ -357,7 +357,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
* @throws Exception * @throws Exception
* @throws Exception * @throws Exception
*/ */
public static function max($field = null): ?ModelInterface public static function max($field = NULL): ?ModelInterface
{ {
$columns = static::makeNewInstance()->getColumns(); $columns = static::makeNewInstance()->getColumns();
if (empty($field)) { if (empty($field)) {
@@ -365,11 +365,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
$columns = $columns->get_fields(); $columns = $columns->get_fields();
if (!isset($columns[$field])) { if (!isset($columns[$field])) {
return null; return NULL;
} }
$first = static::query()->max($field)->first(); $first = static::query()->max($field)->first();
if (empty($first)) { if (empty($first)) {
return null; return NULL;
} }
return $first[$field]; return $first[$field];
} }
@@ -441,11 +441,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
protected static function deleteByCondition($condition = NULL, array $attributes = [], bool $if_condition_is_null = false): bool protected static function deleteByCondition($condition = NULL, array $attributes = [], bool $if_condition_is_null = FALSE): bool
{ {
if (empty($condition)) { if (empty($condition)) {
if (!$if_condition_is_null) { if (!$if_condition_is_null) {
return false; return FALSE;
} }
return (bool)static::query()->delete(); return (bool)static::query()->delete();
} }
@@ -539,7 +539,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
[$sql, $param] = SqlBuilder::builder(static::query())->insert($param); [$sql, $param] = SqlBuilder::builder(static::query())->insert($param);
$dbConnection = $this->getConnection()->createCommand($sql, $param); $dbConnection = $this->getConnection()->createCommand($sql, $param);
$lastId = $dbConnection->save(true); $lastId = $dbConnection->save(TRUE);
$lastId = $this->setPrimary((int)$lastId, $param); $lastId = $this->setPrimary((int)$lastId, $param);
@@ -584,7 +584,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
private function updateInternal($fields, $condition, $param): bool|static private function updateInternal($fields, $condition, $param): bool|static
{ {
if (empty($param)) { if (empty($param)) {
return true; return TRUE;
} }
if ($this->hasPrimary()) { if ($this->hasPrimary()) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()]; $condition = [$this->getPrimary() => $this->getPrimaryValue()];
@@ -594,10 +594,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return $generate; return $generate;
} }
$command = $this->getConnection()->createCommand($generate[0], $generate[1]); $command = $this->getConnection()->createCommand($generate[0], $generate[1]);
if ($command->save(false, $this)) { if ($command->save(FALSE, $this)) {
return $this->refresh()->afterSave($fields, $param); return $this->refresh()->afterSave($fields, $param);
} }
return false; return FALSE;
} }
/** /**
@@ -611,14 +611,15 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$this->_attributes = merge($this->_attributes, $data); $this->_attributes = merge($this->_attributes, $data);
} }
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) { if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return false; return FALSE;
} }
[$change, $condition, $fields] = $this->separation(); [$change, $condition, $fields] = $this->separation();
if (!empty($this->_oldAttributes) || !empty($this->getPrimaryValue())) { if (!empty($this->_oldAttributes)) {
return $this->updateInternal($fields, $condition, $change); return $this->updateInternal($fields, $condition, $change);
} } else {
return $this->insert($change, $fields); return $this->insert($change, $fields);
} }
}
/** /**
@@ -641,7 +642,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function validator(?array $rule): bool public function validator(?array $rule): bool
{ {
if (empty($rule)) return true; if (empty($rule)) return TRUE;
$validate = $this->resolve($rule); $validate = $this->resolve($rule);
if (!$validate->validation()) { if (!$validate->validation()) {
return $this->addError('$validate->getError()', 'mysql'); return $this->addError('$validate->getError()', 'mysql');
@@ -680,7 +681,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
if ($this->hasNote($name)) { if ($this->hasNote($name)) {
return $this->runNote($name, $this->_attributes[$name]); return $this->runNote($name, $this->_attributes[$name]);
} }
return $this->_attributes[$name] ?? null; return $this->_attributes[$name] ?? NULL;
} }
@@ -705,7 +706,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$_tmp = []; $_tmp = [];
$condition = []; $condition = [];
foreach ($this->_attributes as $key => $val) { foreach ($this->_attributes as $key => $val) {
$oldValue = $this->_oldAttributes[$key] ?? null; $oldValue = $this->_oldAttributes[$key] ?? NULL;
if ($val === $oldValue) { if ($val === $oldValue) {
$condition[$key] = $val; $condition[$key] = $val;
} else { } else {
@@ -833,7 +834,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function afterSave($attributes, $changeAttributes): bool public function afterSave($attributes, $changeAttributes): bool
{ {
return true; return TRUE;
} }
@@ -843,7 +844,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function beforeSave($model): bool public function beforeSave($model): bool
{ {
return true; return TRUE;
} }
@@ -882,7 +883,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return $this->{$method}(); return $this->{$method}();
} }
$value = $this->_attributes[$name] ?? null; $value = $this->_attributes[$name] ?? NULL;
return $this->_getter($name, $value); return $this->_getter($name, $value);
} }
@@ -932,7 +933,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
protected function hasNote($name, string $type = self::GET): bool protected function hasNote($name, string $type = self::GET): bool
{ {
if (!isset($this->_annotations[$type])) { if (!isset($this->_annotations[$type])) {
return false; return FALSE;
} }
return isset($this->_annotations[$type][$name]); return isset($this->_annotations[$type][$name]);
} }
@@ -1058,7 +1059,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$model = duplicate(static::class); $model = duplicate(static::class);
$model->_attributes = $data; $model->_attributes = $data;
$model->_oldAttributes = $data; $model->_oldAttributes = $data;
$model->setIsNowExample(false); $model->setIsNowExample(FALSE);
return $model; return $model;
} }