Skip to content

Commit cdeb9b4

Browse files
committed
关联操作增加默认事务
1 parent 3817482 commit cdeb9b4

File tree

1 file changed

+72
-37
lines changed

1 file changed

+72
-37
lines changed

src/Model.php

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -533,18 +533,31 @@ protected function updateData($where)
533533
}
534534
}
535535

536-
// 模型更新
537-
$result = $this->db(false)->where($where)->strict(false)->field($allowFields)->update($data);
536+
$db = $this->db(false);
537+
$db->startTrans();
538538

539-
// 关联更新
540-
if (isset($this->relationWrite)) {
541-
$this->autoRelationUpdate();
542-
}
539+
try {
540+
// 模型更新
541+
$result = $db->where($where)
542+
->strict(false)
543+
->field($allowFields)
544+
->update($data);
545+
546+
// 关联更新
547+
if (isset($this->relationWrite)) {
548+
$this->autoRelationUpdate();
549+
}
543550

544-
// 更新回调
545-
$this->trigger('after_update');
551+
$db->commit();
546552

547-
return $result;
553+
// 更新回调
554+
$this->trigger('after_update');
555+
556+
return $result;
557+
} catch (\Exception $e) {
558+
$db->rollback();
559+
throw $e;
560+
}
548561
}
549562

550563
/**
@@ -568,31 +581,43 @@ protected function insertData($sequence)
568581
// 检查允许字段
569582
$allowFields = $this->checkAllowFields(array_merge($this->auto, $this->insert));
570583

571-
$result = $this->db(false)->strict(false)->field($allowFields)->insert($this->data);
584+
$db = $this->db(false);
585+
$db->startTrans();
572586

573-
// 获取自动增长主键
574-
if ($result && $insertId = $this->db(false)->getLastInsID($sequence)) {
575-
$pk = $this->getPk();
587+
try {
588+
$result = $db->strict(false)
589+
->field($allowFields)
590+
->insert($this->data);
591+
592+
// 获取自动增长主键
593+
if ($result && $insertId = $db->getLastInsID($sequence)) {
594+
$pk = $this->getPk();
576595

577-
foreach ((array) $pk as $key) {
578-
if (!isset($this->data[$key]) || '' == $this->data[$key]) {
579-
$this->data[$key] = $insertId;
596+
foreach ((array) $pk as $key) {
597+
if (!isset($this->data[$key]) || '' == $this->data[$key]) {
598+
$this->data[$key] = $insertId;
599+
}
580600
}
581601
}
582-
}
583602

584-
// 关联写入
585-
if (isset($this->relationWrite)) {
586-
$this->autoRelationInsert();
587-
}
603+
// 关联写入
604+
if (isset($this->relationWrite)) {
605+
$this->autoRelationInsert();
606+
}
588607

589-
// 标记为更新
590-
$this->isUpdate = true;
608+
$db->commit();
591609

592-
// 新增回调
593-
$this->trigger('after_insert');
610+
// 标记为更新
611+
$this->isUpdate = true;
594612

595-
return $result;
613+
// 新增回调
614+
$this->trigger('after_insert');
615+
616+
return $result;
617+
} catch (\Exception $e) {
618+
$db->rollback();
619+
throw $e;
620+
}
596621
}
597622

598623
/**
@@ -738,21 +763,31 @@ public function delete()
738763
// 读取更新条件
739764
$where = $this->getWhere();
740765

741-
// 删除当前模型数据
742-
$result = $this->db(false)->where($where)->delete();
766+
$db = $this->db(false);
767+
$db->startTrans();
743768

744-
// 关联删除
745-
if (!empty($this->relationWrite)) {
746-
$this->autoRelationDelete();
747-
}
769+
try {
770+
// 删除当前模型数据
771+
$result = $db->where($where)->delete();
772+
773+
// 关联删除
774+
if (!empty($this->relationWrite)) {
775+
$this->autoRelationDelete();
776+
}
748777

749-
$this->trigger('after_delete');
778+
$db->commit();
750779

751-
// 清空数据
752-
$this->data = [];
753-
$this->origin = [];
780+
$this->trigger('after_delete');
754781

755-
return $result;
782+
// 清空数据
783+
$this->data = [];
784+
$this->origin = [];
785+
786+
return $result;
787+
} catch (\Exception $e) {
788+
$db->rollback();
789+
throw $e;
790+
}
756791
}
757792

758793
/**

0 commit comments

Comments
 (0)