mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进代码
This commit is contained in:
@@ -293,6 +293,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
return false;
|
||||
}
|
||||
|
||||
$db = self::db();
|
||||
if ($this->isUpdate) {
|
||||
// 自动更新
|
||||
$this->autoCompleteData($this->update);
|
||||
@@ -320,7 +321,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
}
|
||||
}
|
||||
|
||||
$result = self::db()->where($where)->update($data);
|
||||
$result = $db->where($where)->update($data);
|
||||
|
||||
// 更新回调
|
||||
$this->trigger('after_update', $this);
|
||||
@@ -332,11 +333,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = self::db()->insert($this->data);
|
||||
$result = $db->insert($this->data);
|
||||
|
||||
// 获取自动增长主键
|
||||
if ($result && $getId) {
|
||||
$insertId = self::db()->getLastInsID();
|
||||
$insertId = $db->getLastInsID();
|
||||
$pk = $this->getPk();
|
||||
if (is_string($pk) && $insertId) {
|
||||
$this->data[$pk] = $insertId;
|
||||
|
||||
@@ -506,13 +506,14 @@ abstract class Connection
|
||||
*/
|
||||
public function transaction($callback)
|
||||
{
|
||||
$this->startTrans(NOW_TIME);
|
||||
$label = microtime(true);
|
||||
$this->startTrans($label);
|
||||
try {
|
||||
$result = null;
|
||||
if (is_callable($callback)) {
|
||||
$result = call_user_func_array($callback, []);
|
||||
}
|
||||
$this->commit(NOW_TIME);
|
||||
$this->commit($label);
|
||||
return $result;
|
||||
} catch (\PDOException $e) {
|
||||
$this->rollback();
|
||||
|
||||
@@ -166,7 +166,8 @@ class Merge extends Model
|
||||
// 处理模型数据
|
||||
$data = $this->parseData($this->name, $this->data);
|
||||
|
||||
self::db()->startTrans();
|
||||
$db = self::db();
|
||||
$db->startTrans('merge_save_' . $this->name);
|
||||
try {
|
||||
if ($this->isUpdate) {
|
||||
// 自动写入
|
||||
@@ -177,15 +178,15 @@ class Merge extends Model
|
||||
}
|
||||
|
||||
// 写入主表数据
|
||||
$result = self::db()->strict(false)->update($data);
|
||||
$result = $db->strict(false)->update($data);
|
||||
|
||||
// 写入附表数据
|
||||
foreach (static::$relationModel as $key => $model) {
|
||||
$name = is_int($key) ? $model : $key;
|
||||
$table = is_int($key) ? self::db()->getTable($model) : $model;
|
||||
$table = is_int($key) ? $db->getTable($model) : $model;
|
||||
// 处理关联模型数据
|
||||
$data = $this->parseData($name, $this->data);
|
||||
$query = clone self::db();
|
||||
$query = clone $db;
|
||||
$query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data);
|
||||
}
|
||||
// 新增回调
|
||||
@@ -199,19 +200,19 @@ class Merge extends Model
|
||||
}
|
||||
|
||||
// 写入主表数据
|
||||
$result = self::db()->name($this->name)->strict(false)->insert($this->data);
|
||||
$result = $db->name($this->name)->strict(false)->insert($this->data);
|
||||
if ($result) {
|
||||
$insertId = self::db()->getLastInsID();
|
||||
$insertId = $db->getLastInsID();
|
||||
// 写入外键数据
|
||||
$this->data[$this->fk] = $insertId;
|
||||
|
||||
// 写入附表数据
|
||||
foreach (static::$relationModel as $key => $model) {
|
||||
$name = is_int($key) ? $model : $key;
|
||||
$table = is_int($key) ? self::db()->getTable($model) : $model;
|
||||
$table = is_int($key) ? $db->getTable($model) : $model;
|
||||
// 处理关联模型数据
|
||||
$data = $this->parseData($name, $this->data, true);
|
||||
$query = clone self::db();
|
||||
$query = clone $db;
|
||||
$query->table($table)->strict(false)->insert($data);
|
||||
}
|
||||
$result = $insertId;
|
||||
@@ -219,10 +220,10 @@ class Merge extends Model
|
||||
// 新增回调
|
||||
$this->trigger('after_insert', $this);
|
||||
}
|
||||
self::db()->commit();
|
||||
$db->commit('merge_save_' . $this->name);
|
||||
return $result;
|
||||
} catch (\PDOException $e) {
|
||||
self::db()->rollback();
|
||||
$db->rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -237,25 +238,26 @@ class Merge extends Model
|
||||
if (false === $this->trigger('before_delete', $this)) {
|
||||
return false;
|
||||
}
|
||||
self::db()->startTrans();
|
||||
$db = self::db();
|
||||
$db->startTrans('merge_delete_' . $this->name);
|
||||
try {
|
||||
$result = self::db()->delete($this->data);
|
||||
$result = $db->delete($this->data);
|
||||
if ($result) {
|
||||
// 获取主键数据
|
||||
$pk = $this->data[$this->getPk()];
|
||||
|
||||
// 删除关联数据
|
||||
foreach (static::$relationModel as $key => $model) {
|
||||
$table = is_int($key) ? self::db()->getTable($model) : $model;
|
||||
$query = clone self::db();
|
||||
$table = is_int($key) ? $db->getTable($model) : $model;
|
||||
$query = clone $db;
|
||||
$query->table($table)->where($this->fk, $pk)->delete();
|
||||
}
|
||||
}
|
||||
$this->trigger('after_delete', $this);
|
||||
self::db()->commit();
|
||||
$db->commit('merge_delete_' . $this->name);
|
||||
return $result;
|
||||
} catch (\PDOException $e) {
|
||||
self::db()->rollback();
|
||||
$db->rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user