博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii框架开启事务
阅读量:4626 次
发布时间:2019-06-09

本文共 1362 字,大约阅读时间需要 4 分钟。

public function actionAdd()    {        $model = new Goods();        $model->setScenario('insert'); if ($model->load(Yii::$app->request->post())) { $model->image = UploadedFile::getInstance($model, 'image'); if ($model->validate()) { $model->cover = Yii::$app->security->generateRandomString(10) . '.' . $model->image->extension; if (floatval($model->price_original) < 0.01) { $model->price_original = null; } $transaction = Yii::$app->db->beginTransaction(); try { if (!$model->save(false)) { throw new \Exception('商品添加失败!'); } $filename = Yii::getAlias(Yii::$app->params['goods.coverPath']) . DIRECTORY_SEPARATOR . $model->cover; if (!$model->image->saveAs($filename)) { throw new \Exception('封面图片添加失败!'); } // 记录商品库存 $goodsSurplus = new GoodsSurplus(); $goodsSurplus->goods_id = $model->id; $goodsSurplus->surplus_before = 0; $goodsSurplus->amount = $model->surplus; $goodsSurplus->surplus_after = $model->surplus; $goodsSurplus->remark = '初始化库存。'; if (!$goodsSurplus->save(false)) { throw new \Exception('商品库存记录失败!'); } $transaction->commit(); Yii::$app->session->setFlash('success', '成功添加商品“'.$model->name.'”。'); return $this->refresh(); } catch (\Exception $e) { $transaction->rollBack(); Yii::$app->session->setFlash('danger', $e->getMessage()); } } } return $this->render('form', [ 'model' => $model ]); }

转载于:https://www.cnblogs.com/ghjbk/p/6745171.html

你可能感兴趣的文章
pyqt图标
查看>>
ASCII码对照表
查看>>
很棒的积极自我暗示语
查看>>
《linux系统及其编程》实验课记录(一)
查看>>
本学期(大三下学期)学习目标
查看>>
painting fence - 分治 - Codeforces 448c
查看>>
游戏模型规范
查看>>
【转】gcc编译优化---likely()与unlikely()函数的意义
查看>>
完成评论功能
查看>>
HDOJ2567 ( 寻梦 ) 【切水题,很欢乐~】
查看>>
Struts2方法调用的三种方式
查看>>
Navicat工具多表查询
查看>>
第四章 读书笔记
查看>>
我不为人人,人人不为我
查看>>
iOS网络编程(三) 异步加载及缓存图片---->SDWebImage
查看>>
Qt qml 模拟iphone slide to unlock 的聚光动画文字效果
查看>>
c++11 std::move() 的使用
查看>>
HDU 4607 Park Visit (DP最长链)
查看>>
实例学架构设计之源起复杂度
查看>>
leetcode- Rotate Array 旋转数组
查看>>