From 051627547e5b4965a6aa0baafa9e326b7657ea0b Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Tue, 30 Jun 2020 08:40:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BE=AE=E4=BF=A1=E5=9B=BE?= =?UTF-8?q?=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/wechat/controller/Graphic.php | 2 +- .../repository/WechatGraphicRepository.php | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/catch/wechat/controller/Graphic.php b/catch/wechat/controller/Graphic.php index 4ff7fca..55d07a5 100644 --- a/catch/wechat/controller/Graphic.php +++ b/catch/wechat/controller/Graphic.php @@ -51,7 +51,7 @@ class Graphic extends CatchController return CatchResponse::success($this->graphic->storeBy($request->param())); } - public function update($id, Request $request) + public function update($id, CatchRequest $request) { return CatchResponse::success($this->graphic->updateBy($id, $request->param())); } diff --git a/catch/wechat/repository/WechatGraphicRepository.php b/catch/wechat/repository/WechatGraphicRepository.php index 512b012..782b79e 100644 --- a/catch/wechat/repository/WechatGraphicRepository.php +++ b/catch/wechat/repository/WechatGraphicRepository.php @@ -111,22 +111,26 @@ class WechatGraphicRepository extends CatchRepository */ public function updateBy(int $id, array $data) { - $articles = $data['articles']; - $this->wechatGraphic->startTrans(); - $updateStatus = true; - foreach ($articles as $article) { - $id = $article['id']; - unset($article['id']); - if ($this->wechatGraphic->where('id', $id) - ->update($article) === false) { - $updateStatus = false; - break; + try { + $this->wechatGraphic->startTrans(); + if (!parent::deleteBy($id)) { + throw new FailedException('更新失败'); } - } - if (!$updateStatus) { + if ($this->wechatGraphic->where('parent_id', $id)->find() && !$this->wechatGraphic->where('parent_id', $id)->delete()) { + throw new FailedException('更新失败'); + } + + foreach ($data['articles'] as &$article) { + unset($article['id']); + } + + if ($this->storeBy($data) === false) { + throw new FailedException('更新失败'); + } + } catch (\Exception $exception) { $this->wechatGraphic->rollback(); - throw new FailedException('更新失败'); + throw new FailedException($exception->getMessage()); } $this->wechatGraphic->commit();