yii2事务操作

$customer = Customer::findOne(123);

Customer::getDb()->transaction(function($db) use ($customer) {

$customer->id = 200;

$customer->save();

// ...其他 DB 操作...});

// 或者

$transaction = Customer::getDb()->beginTransaction();

try {

$customer->id = 200;

$customer->save();

// ...other DB operations...

$transaction->commit();

} catch(\Exception $e) {

$transaction->rollBack();

throw $e;

} catch(\Throwable $e) {

$transaction->rollBack();

throw $e;

}

你可能感兴趣的:(yii,php)