更新组件

This commit is contained in:
巴掌 committed 2020-10-15 19:32:57 +08:00
1 parent 5319384a34
commit eb970fd38f
779 files changed
+19480 -75705

No files matched your search

+74 -54
View File
@@ -139,6 +139,12 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
*/
protected static $maker = [];
/**
* 方法注入
* @var Closure[][]
*/
protected static $macro = [];
/**
* 设置服务注入
* @access public
@@ -150,6 +156,21 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
static::$maker[] = $maker;
}
/**
* 设置方法注入
* @access public
* @param string $method
* @param Closure $closure
* @return void
*/
public static function macro(string $method, Closure $closure)
{
if (!isset(static::$macro[static::class])) {
static::$macro[static::class] = [];
}
static::$macro[static::class][$method] = $closure;
}
/**
* 设置Db对象
* @access public
@@ -611,9 +632,8 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
// 模型更新
$db = $this->db();
$db->startTrans();
try {
$db->transaction(function () use ($data, $allowFields, $db) {
$this->key = null;
$where = $this->getWhere();
@@ -630,17 +650,12 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
if (!empty($this->relationWrite)) {
$this->autoRelationUpdate();
}
});
$db->commit();
// 更新回调
$this->trigger('AfterUpdate');
// 更新回调
$this->trigger('AfterUpdate');
return true;
} catch (\Exception $e) {
$db->rollback();
throw $e;
}
return true;
}
/**
@@ -672,9 +687,8 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
$allowFields = $this->checkAllowFields();
$db = $this->db();
$db->startTrans();
try {
$db->transaction(function () use ($sequence, $allowFields, $db) {
$result = $db->strict(false)
->field($allowFields)
->replace($this->replace)
@@ -694,20 +708,15 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
if (!empty($this->relationWrite)) {
$this->autoRelationInsert();
}
});
$db->commit();
// 标记数据已经存在
$this->exists = true;
// 标记数据已经存在
$this->exists = true;
// 新增回调
$this->trigger('AfterInsert');
// 新增回调
$this->trigger('AfterInsert');
return true;
} catch (\Exception $e) {
$db->rollback();
throw $e;
}
return true;
}
/**
@@ -748,9 +757,9 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
public function saveAll(iterable $dataSet, bool $replace = true): Collection
{
$db = $this->db();
$db->startTrans();
try {
$result = $db->transaction(function () use ($replace, $dataSet) {
$pk = $this->getPk();
if (is_string($pk) && $replace) {
@@ -759,21 +768,20 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
$result = [];
$suffix = $this->getSuffix();
foreach ($dataSet as $key => $data) {
if ($this->exists || (!empty($auto) && isset($data[$pk]))) {
$result[$key] = self::update($data);
$result[$key] = static::update($data, [], [], $suffix);
} else {
$result[$key] = self::create($data, $this->field, $this->replace);
$result[$key] = static::create($data, $this->field, $this->replace, $suffix);
}
}
$db->commit();
return $result;
});
return $this->toCollection($result);
} catch (\Exception $e) {
$db->rollback();
throw $e;
}
return $this->toCollection($result);
}
/**
@@ -791,9 +799,8 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
$where = $this->getWhere();
$db = $this->db();
$db->startTrans();
try {
$db->transaction(function () use ($where, $db) {
// 删除当前模型数据
$db->where($where)->delete();
@@ -801,30 +808,26 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
if (!empty($this->relationWrite)) {
$this->autoRelationDelete();
}
});
$db->commit();
$this->trigger('AfterDelete');
$this->trigger('AfterDelete');
$this->exists = false;
$this->lazySave = false;
$this->exists = false;
$this->lazySave = false;
return true;
} catch (\Exception $e) {
$db->rollback();
throw $e;
}
return true;
}
/**
* 写入数据
* @access public
* @param array $data 数据数组
* @param array $allowField 允许字段
* @param bool $replace 使用Replace
* @param array $data 数据数组
* @param array $allowField 允许字段
* @param bool $replace 使用Replace
* @param string $suffix 数据表后缀
* @return static
*/
public static function create(array $data, array $allowField = [], bool $replace = false): Model
public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
{
$model = new static();
@@ -832,6 +835,10 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
$model->allowField($allowField);
}
if (!empty($suffix)) {
$model->setSuffix($suffix);
}
$model->replace($replace)->save($data);
return $model;
@@ -840,12 +847,13 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
/**
* 更新数据
* @access public
* @param array $data 数据数组
* @param mixed $where 更新条件
* @param array $allowField 允许字段
* @param array $data 数据数组
* @param mixed $where 更新条件
* @param array $allowField 允许字段
* @param string $suffix 数据表后缀
* @return static
*/
public static function update(array $data, $where = [], array $allowField = [])
public static function update(array $data, $where = [], array $allowField = [], string $suffix = '')
{
$model = new static();
@@ -857,6 +865,10 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
$model->setUpdateWhere($where);
}
if (!empty($suffix)) {
$model->setSuffix($suffix);
}
$model->exists(true)->save($data);
return $model;
@@ -1013,6 +1025,10 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
public function __call($method, $args)
{
if (isset(static::$macro[static::class][$method])) {
return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
}
if ('withattr' == strtolower($method)) {
return call_user_func_array([$this, 'withAttribute'], $args);
}
@@ -1022,6 +1038,10 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
public static function __callStatic($method, $args)
{
if (isset(static::$macro[static::class][$method])) {
return call_user_func_array(static::$macro[static::class][$method]->bindTo(null, static::class), $args);
}
$model = new static();
return call_user_func_array([$model->db(), $method], $args);