在 Yii2 中,一个 update 接口,如果在执行 save() 之前,表中的记录已经被删除,则仍然会返回 ture,是否需要避免此种情况
1、在 Yii2 中,一个 update 接口,如果在执行 save() 之前,表中的记录已经被删除,则仍然会返回 ture,是否需要避免此种情况。如图1
2、代码实现如下
$animation = InvitationPageElementAnimation::find()->where([
'id' => $id,
])->with(['invitation'])->limit(1)->one();
$executeRes = Yii::$app->db->createCommand('DELETE FROM `invitation_page_element_animations` WHERE id=:id')
->bindValue(':id', $id)
->execute();
Yii::info(
[
$executeRes
],
'$executeRes'
);
try {
$res = $animation->save(false);
Yii::info(
[
$res
],
'$res'
);
if (!$res) {
return [
'code' => 10400,
'message' => '设置动画属性失败',
];
}
return [
'code' => 10000,
'message' => '设置动画属性成功',
'data' => $animation,
];
} catch (Throwable $e) {
Yii::error([
'exception' => get_class($e),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
], 'application.model.save.exception');
return [
'code' => 10500,
'message' => '数据库操作失败,请联系管理员',
];
}
3、决定暂时不调整。

近期评论