This commit is contained in:
2021-07-12 18:15:58 +08:00
parent 98a39dff91
commit 16b300b139
4 changed files with 32 additions and 113 deletions
+19 -100
View File
@@ -28,7 +28,6 @@ use ReflectionException;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Abstracts\TraitApplication; use Snowflake\Abstracts\TraitApplication;
use Snowflake\Event as SEvent;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -135,36 +134,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} }
/**
* @param $name
* @param $method
* @return static
*/
public function addGets($name, $method): static
{
if (!isset($this->_annotations[self::GET])) {
$this->_annotations[self::GET] = [];
}
$this->_annotations[self::GET][$name] = [$this, $method];
return $this;
}
/**
* @param $name
* @param $method
* @return static
*/
public function addSets($name, $method): static
{
if (!isset($this->_annotations[self::SET])) {
$this->_annotations[self::SET] = [];
}
$this->_annotations[self::SET][$name] = [$this, $method];
return $this;
}
/** /**
* @return array * @return array
*/ */
@@ -173,6 +142,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
return $this->actions; return $this->actions;
} }
/** /**
* @return bool * @return bool
*/ */
@@ -181,11 +151,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
return $this->isNewExample === TRUE; return $this->isNewExample === TRUE;
} }
/** /**
* @param bool $bool * @param bool $bool
* @return $this * @return $this
*/ */
public function setIsCreate($bool = FALSE): static public function setIsCreate(bool $bool = FALSE): static
{ {
$this->isNewExample = $bool; $this->isNewExample = $bool;
return $this; return $this;
@@ -201,6 +172,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
return Snowflake::app()->getLogger()->getLastError('mysql'); return Snowflake::app()->getLogger()->getLastError('mysql');
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
@@ -217,6 +189,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
return false; return false;
} }
/** /**
* @throws Exception * @throws Exception
*/ */
@@ -336,7 +309,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public static function deleteByCondition($condition = NULL, $attributes = [], $if_condition_is_null = false): bool public 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) {
@@ -426,28 +399,15 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
private function insert($param, $attributes): bool|static private function insert($param, $attributes): bool|static
{ {
if (empty($param)) {
return FALSE;
}
$dbConnection = static::getDb();
[$sql, $param] = SqlBuilder::builder(static::find())->insert($param); [$sql, $param] = SqlBuilder::builder(static::find())->insert($param);
$dbConnection = static::getDb()->createCommand($sql, static::getDbName(), $param);
// $trance = $dbConnection->beginTransaction(); if (!($lastId = (int)$dbConnection->save(true, $this))) {
try {
if (!($lastId = (int)$dbConnection->createCommand($sql, static::getDbName(), $param)->save(true, $this))) {
throw new Exception('保存失败.'); throw new Exception('保存失败.');
} }
// $trance->commit();
$lastId = $this->setPrimary($lastId, $param); $lastId = $this->setPrimary($lastId, $param);
$this->refresh(); $this->refresh()->afterSave($attributes, $param);
SEvent::trigger(self::AFTER_SAVE, [$attributes, $param]);
} catch (\Throwable $exception) {
// $trance->rollback();
$lastId = $this->addError($exception, 'mysql');
}
return $lastId; return $lastId;
} }
@@ -486,33 +446,18 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
private function updateInternal($fields, $condition, $param): bool|static private function updateInternal($fields, $condition, $param): bool|static
{ {
if (empty($param)) {
return true;
}
$command = static::getDb();
if ($this->hasPrimary()) { if ($this->hasPrimary()) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()]; $condition = [$this->getPrimary() => $this->getPrimaryValue()];
} }
$generate = SqlBuilder::builder(static::find()->where($condition))->update($param); $generate = SqlBuilder::builder(static::find()->where($condition))->update($param);
if (is_bool($generate)) { if (is_bool($generate)) {
return $generate; return $generate;
} }
$command = static::getDb()->createCommand($generate[0], static::getDbName(), $generate[1]);
// $trance = $command->beginTransaction(); if ($command->save(false, $this)) {
if (!$command->createCommand($generate[0], static::getDbName(), $generate[1])->save(false, $this)) { return $this->refresh()->afterSave($fields, $param);
// $trance->rollback();
return false;
} }
return false;
// $trance->commit();
$this->refresh();
SEvent::trigger(self::AFTER_SAVE, [$fields, $param]);
return true;
} }
/** /**
@@ -525,23 +470,19 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
if (!is_null($data)) { if (!is_null($data)) {
$this->_attributes = merge($this->_attributes, $data); $this->_attributes = merge($this->_attributes, $data);
} }
if (!$this->validator($this->rules())) { if (!$this->validator($this->rules())) {
return false; return false;
} }
if ($this->beforeSave($this)) {
if (!SEvent::trigger(self::BEFORE_SAVE, [$this], $this)) {
return false;
}
static::getDb()->enablingTransactions(); static::getDb()->enablingTransactions();
[$change, $condition, $fields] = $this->filtration_and_separation(); [$change, $condition, $fields] = $this->filtration_and_separation();
if (!$this->isNewExample) { if (!$this->isNewExample) {
return $this->updateInternal($fields, $condition, $change); return $this->updateInternal($fields, $condition, $change);
} }
return $this->insert($change, $fields); return $this->insert($change, $fields);
} }
return false;
}
/** /**
@@ -614,19 +555,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
{ {
$_tmp = []; $_tmp = [];
$condition = []; $condition = [];
$columns = static::getColumns();
$format = $columns->getAllField();
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) {
$_tmp[$key] = $this->toFormat($columns, $format, $key, $val);
if (is_bool($_tmp[$key])) {
unset($_tmp[$key]);
}
} else {
$condition[$key] = $val; $condition[$key] = $val;
} else {
$_tmp[$key] = $val;
} }
} }
return [$_tmp, $condition, array_keys($_tmp)]; return [$_tmp, $condition, array_keys($_tmp)];
@@ -837,21 +771,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
* @throws Exception * @throws Exception
*/ */
public function __get($name): mixed public function __get($name): mixed
{
if (Snowflake::app()->has($name)) {
return Snowflake::getApp($name);
} else {
return $this->_gets($name);
}
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
private function _gets($name): mixed
{ {
$value = $this->_attributes[$name] ?? null; $value = $this->_attributes[$name] ?? null;
$method = $this->_get_annotation($name, static::GET); $method = $this->_get_annotation($name, static::GET);
+1 -1
View File
@@ -45,7 +45,7 @@ class Biomonitoring extends Process
$server = Snowflake::app()->getSwoole(); $server = Snowflake::app()->getSwoole();
Timer::tick(1000, function () use ($server) { Timer::tick(1000, function () use ($server) {
clearstatcache(); clearstatcache();
if (($size = filesize($server->setting['log_file'])) > 1024000000) { if (filesize($server->setting['log_file']) > 1024000000) {
@unlink($server->setting['log_file']); @unlink($server->setting['log_file']);
Process::kill($server->master_pid, SIGRTMIN); Process::kill($server->master_pid, SIGRTMIN);
} }
+2 -2
View File
@@ -82,7 +82,7 @@ class ServerInotify extends Process
* @param bool $isReload * @param bool $isReload
* @throws Exception * @throws Exception
*/ */
private function loadDirs($isReload = false) private function loadDirs(bool $isReload = false)
{ {
foreach ($this->dirs as $value) { foreach ($this->dirs as $value) {
if (is_bool($path = realpath($value))) { if (is_bool($path = realpath($value))) {
@@ -120,7 +120,7 @@ class ServerInotify extends Process
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
private function loadByDir($path, $isReload = false): void private function loadByDir($path, bool $isReload = false): void
{ {
if (!is_string($path)) { if (!is_string($path)) {
return; return;
+1 -1
View File
@@ -269,7 +269,7 @@ if (!function_exists('write')) {
* @param string $category * @param string $category
* @throws Exception * @throws Exception
*/ */
function write(string $messages, $category = 'app') function write(string $messages, string $category = 'app')
{ {
$logger = Snowflake::app()->getLogger(); $logger = Snowflake::app()->getLogger();
$logger->write($messages, $category); $logger->write($messages, $category);