改进代码

This commit is contained in:
thinkphp
2016-05-17 18:19:13 +08:00
parent baebe7899e
commit 63521c1db3
3 changed files with 25 additions and 21 deletions

View File

@@ -293,6 +293,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false; return false;
} }
$db = self::db();
if ($this->isUpdate) { if ($this->isUpdate) {
// 自动更新 // 自动更新
$this->autoCompleteData($this->update); $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); $this->trigger('after_update', $this);
@@ -332,11 +333,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false; return false;
} }
$result = self::db()->insert($this->data); $result = $db->insert($this->data);
// 获取自动增长主键 // 获取自动增长主键
if ($result && $getId) { if ($result && $getId) {
$insertId = self::db()->getLastInsID(); $insertId = $db->getLastInsID();
$pk = $this->getPk(); $pk = $this->getPk();
if (is_string($pk) && $insertId) { if (is_string($pk) && $insertId) {
$this->data[$pk] = $insertId; $this->data[$pk] = $insertId;

View File

@@ -506,13 +506,14 @@ abstract class Connection
*/ */
public function transaction($callback) public function transaction($callback)
{ {
$this->startTrans(NOW_TIME); $label = microtime(true);
$this->startTrans($label);
try { try {
$result = null; $result = null;
if (is_callable($callback)) { if (is_callable($callback)) {
$result = call_user_func_array($callback, []); $result = call_user_func_array($callback, []);
} }
$this->commit(NOW_TIME); $this->commit($label);
return $result; return $result;
} catch (\PDOException $e) { } catch (\PDOException $e) {
$this->rollback(); $this->rollback();

View File

@@ -166,7 +166,8 @@ class Merge extends Model
// 处理模型数据 // 处理模型数据
$data = $this->parseData($this->name, $this->data); $data = $this->parseData($this->name, $this->data);
self::db()->startTrans(); $db = self::db();
$db->startTrans('merge_save_' . $this->name);
try { try {
if ($this->isUpdate) { 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) { foreach (static::$relationModel as $key => $model) {
$name = is_int($key) ? $model : $key; $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); $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); $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) { if ($result) {
$insertId = self::db()->getLastInsID(); $insertId = $db->getLastInsID();
// 写入外键数据 // 写入外键数据
$this->data[$this->fk] = $insertId; $this->data[$this->fk] = $insertId;
// 写入附表数据 // 写入附表数据
foreach (static::$relationModel as $key => $model) { foreach (static::$relationModel as $key => $model) {
$name = is_int($key) ? $model : $key; $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); $data = $this->parseData($name, $this->data, true);
$query = clone self::db(); $query = clone $db;
$query->table($table)->strict(false)->insert($data); $query->table($table)->strict(false)->insert($data);
} }
$result = $insertId; $result = $insertId;
@@ -219,10 +220,10 @@ class Merge extends Model
// 新增回调 // 新增回调
$this->trigger('after_insert', $this); $this->trigger('after_insert', $this);
} }
self::db()->commit(); $db->commit('merge_save_' . $this->name);
return $result; return $result;
} catch (\PDOException $e) { } catch (\PDOException $e) {
self::db()->rollback(); $db->rollback();
return false; return false;
} }
} }
@@ -237,25 +238,26 @@ class Merge extends Model
if (false === $this->trigger('before_delete', $this)) { if (false === $this->trigger('before_delete', $this)) {
return false; return false;
} }
self::db()->startTrans(); $db = self::db();
$db->startTrans('merge_delete_' . $this->name);
try { try {
$result = self::db()->delete($this->data); $result = $db->delete($this->data);
if ($result) { if ($result) {
// 获取主键数据 // 获取主键数据
$pk = $this->data[$this->getPk()]; $pk = $this->data[$this->getPk()];
// 删除关联数据 // 删除关联数据
foreach (static::$relationModel as $key => $model) { foreach (static::$relationModel as $key => $model) {
$table = is_int($key) ? self::db()->getTable($model) : $model; $table = is_int($key) ? $db->getTable($model) : $model;
$query = clone self::db(); $query = clone $db;
$query->table($table)->where($this->fk, $pk)->delete(); $query->table($table)->where($this->fk, $pk)->delete();
} }
} }
$this->trigger('after_delete', $this); $this->trigger('after_delete', $this);
self::db()->commit(); $db->commit('merge_delete_' . $this->name);
return $result; return $result;
} catch (\PDOException $e) { } catch (\PDOException $e) {
self::db()->rollback(); $db->rollback();
return false; return false;
} }
} }