在PHP 7.1 、Yii 2 中,AR 活动记录事务,需要同时操作多个模型的实现
1、在PHP 7.1 、Yii 2 中,AR 活动记录事务,需要同时操作多个模型的实现,如图1
$transaction = $this->db->beginTransaction(); try { if (!$this->save()) { throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); } /* 创建MySQL模型(内容详情) */ $contentDetail = new ContentDetail(); $contentDetail->tenantid = $this->tenantid; $contentDetail->content_audit_id = $this->id; $contentDetail->content = $this->content; if (!$contentDetail->save()) { throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); } $transaction->commit(); } catch(\Throwable $e) { $transaction->rollBack(); throw $e; }
2、测试事务是否支持,特意让$this->save()操作失败、让$contentDetail->save()操作成功,响应如下,如图2
{ "name": "Database Exception", "message": "SQLSTATE[HY000]: General error: 1364 Field 'updated_at' doesn't have a default value\nThe SQL being executed was: INSERT INTO `ca_content_audit` (`code`, `data_source_detail_id`, `source`, `source_tenantid`, `data_source_code`, `content_type`, `logo`, `title`, `publish_date`, `tenantid`, `audit_process_id`, `step`, `uid`, `uid_all`, `audit_status`, `call_back`, `created_at`) VALUES ('shuangliu', '1', '双流', 'shuangliu', 'share', 1, 'logo', 'title', '2017-03-03 12:05:54', 'default', 1, 0, '', '', 2, '', '2018-03-07 13:38:29')", "code": 0, "type": "yii\\db\\Exception", "file": "E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\Schema.php", "line": 595, "stack-trace": [ "#0 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\Command.php(1004): yii\\db\\Schema->convertException(Object(PDOException), 'INSERT INTO `ca...')", "#1 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\Schema.php(412): yii\\db\\Command->execute()", "#2 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\ActiveRecord.php(511): yii\\db\\Schema->insert('{{%content_audi...', Array)", "#3 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\ActiveRecord.php(477): yii\\db\\ActiveRecord->insertInternal(NULL)", "#4 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\BaseActiveRecord.php(646): yii\\db\\ActiveRecord->insert(true, NULL)", "#5 E:\\wwwroot\\cmcp-api\\api\\models\\ContentAudit.php(121): yii\\db\\BaseActiveRecord->save()", "#6 E:\\wwwroot\\cmcp-api\\api\\rests\\content_audit\\CreateAction.php(55): api\\models\\ContentAudit->create()", "#7 [internal function]: api\\rests\\content_audit\\CreateAction->run()", "#8 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\base\\Action.php(94): call_user_func_array(Array, Array)", "#9 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\base\\Controller.php(157): yii\\base\\Action->runWithParams(Array)", "#10 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('create', Array)", "#11 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('v1/content-audi...', Array)", "#12 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))", "#13 E:\\wwwroot\\cmcp-api\\api\\web\\index.php(23): yii\\base\\Application->run()", "#14 {main}" ], "error-info": [ "HY000", 1364, "Field 'updated_at' doesn't have a default value" ], "previous": { "name": "Exception", "message": "SQLSTATE[HY000]: General error: 1364 Field 'updated_at' doesn't have a default value", "code": "HY000", "type": "PDOException", "file": "E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\Command.php", "line": 994, "stack-trace": [ "#0 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\Command.php(994): PDOStatement->execute()", "#1 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\Schema.php(412): yii\\db\\Command->execute()", "#2 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\ActiveRecord.php(511): yii\\db\\Schema->insert('{{%content_audi...', Array)", "#3 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\ActiveRecord.php(477): yii\\db\\ActiveRecord->insertInternal(NULL)", "#4 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\db\\BaseActiveRecord.php(646): yii\\db\\ActiveRecord->insert(true, NULL)", "#5 E:\\wwwroot\\cmcp-api\\api\\models\\ContentAudit.php(121): yii\\db\\BaseActiveRecord->save()", "#6 E:\\wwwroot\\cmcp-api\\api\\rests\\content_audit\\CreateAction.php(55): api\\models\\ContentAudit->create()", "#7 [internal function]: api\\rests\\content_audit\\CreateAction->run()", "#8 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\base\\Action.php(94): call_user_func_array(Array, Array)", "#9 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\base\\Controller.php(157): yii\\base\\Action->runWithParams(Array)", "#10 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('create', Array)", "#11 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('v1/content-audi...', Array)", "#12 E:\\wwwroot\\cmcp-api\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))", "#13 E:\\wwwroot\\cmcp-api\\api\\web\\index.php(23): yii\\base\\Application->run()", "#14 {main}" ] } }
3、查看数据库,此时$this、$contentDetail皆未执行成功,如图3
4、测试事务是否支持,特意让$this->save()操作成功、让$contentDetail->save()操作失败,响应如下,如图4
{ "name": "Internal Server Error", "message": "Failed to create the object for unknown reason.", "code": 0, "status": 500, "type": "yii\\web\\ServerErrorHttpException" }
5、查看数据库,此时$this、$contentDetail皆未执行成功,如图5
6、测试事务是否支持,特意让$this->save()操作成功、让$contentDetail->save()操作成功,响应如下,如图6
7、查看数据库,此时$this、$contentDetail皆执行成功,如图7
8、可以继续新增模型
$transaction = $this->db->beginTransaction(); try { if (!$this->save()) { throw new ServerErrorHttpException(Yii::t('error', '20032'), 20032); } /* 创建MySQL模型(内容详情) */ $contentDetail = new ContentDetail(); $contentDetail->tenantid = $this->tenantid; $contentDetail->content_audit_id = $this->id; $contentDetail->content = $this->content; if (!$contentDetail->save()) { throw new ServerErrorHttpException(Yii::t('error', '20033'), 20033); } /* 创建MySQL模型(审核记录) */ $auditLog = new AuditLog(); $auditLog->tenantid = $this->tenantid; $auditLog->content_audit_id = $this->id; $auditLog->step = self::AUDIT_PROCESS_STEP_AIR; $auditLog->operation_user = ''; $auditLog->operation_user_id = 0; $auditLog->cause = ''; $auditLog->status = self::STATUS_ACTIVE; $auditLog->created_at = $time; if (!$auditLog->save()) { throw new ServerErrorHttpException(Yii::t('error', '20034'), 20034); } $transaction->commit(); } catch(\Throwable $e) { $transaction->rollBack(); throw $e; }
近期评论