本文介绍了在Yii中循环使用事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个活动记录数组,并希望以这种方式通过循环更改它们的某些字段:
I have an array of active records and want to change some field of them with a loop in this manner:
$error = false;
foreach ($items as $item) {
$item->is_paid = self::PENDING;
$error = $error || !$item->save();
}
return $error;
我想要做的就是更改所有这些项目的is_paid
属性.如果打开失败,请回滚其他的.我该如何使用交易来解决这个问题?
What I want to do is to change the is_paid
property for all of this items. If on fails, roll back the others. How can I use transaction to solve this problem?
推荐答案
简要介绍在这里,我能够在yii中找到事务管理,类似以下的内容应该对您有用:
By a brief look here, I was able to find the transaction management in yii, something like the following should work for you:
$transaction = Yii::app()->db->beginTransaction();
try {
foreach ($items as $item) {
$item->is_paid = self::PENDING;
$item->save();
}
$transaction->commit();
// actions to do on success (redirect, alert, etc.)
} catch (Exception $e) {
$transaction->rollBack();
// other actions to perform on fail (redirect, alert, etc.)
}
这篇关于在Yii中循环使用事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!