In PHP 7.1 and Yii 2, AR activity records transactions, you need to operate multiple models at the same time
1. In PHP 7.1 and Yii 2, AR activity records transactions, and the implementation of multiple models needs to be operated at the same time, as shown in Figure 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. Test whether the transaction is supported, and let the $this->save() operation fail, and let the $contentDetail->save() operation succeed, the response is as follows, as shown in Figure 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. Check the database. At this time, $this and $contentDetail have not been successfully executed, as shown in Figure 3
4. Test whether the transaction is supported, and make the $this->save() operation successful, and let the $contentDetail->save() operation fail, the response is as follows, as shown in Figure 4
{
"name": "Internal Server Error",
"message": "Failed to create the object for unknown reason.",
"code": 0,
"status": 500,
"type": "yii\\web\\ServerErrorHttpException"
}
5. Check the database, at this time, both $this and $contentDetail have not been successfully executed, as shown in Figure 5
6. Test whether the transaction is supported, and make the $this->save() operation successful, and let the $contentDetail->save() operation succeed, the response is as follows, as shown in Figure 6
7. Check the database, at this time, both $this and $contentDetail are successfully executed, as shown in Figure 7
8. You can continue to add new models
$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;
}






