MySQL transactions use three primitives:
php
beginTransaction() // start
commit() // commit
rollBack() // rollbackExample: create an order and decrement stock; on any failure roll back.
php
$model = m('order');
$model->beginTransaction();
try {
$data = array(
'user_id' => 'demo',
'price' => 150.00,
);
$id = $model->insert($data);
if (!$id) {
throw new \Exception('add order err');
}
$re = m('goods')->where(array('goods_id' => 1))->setDec('num', 1);
if (!$re) {
throw new \Exception('update goods num err');
}
$model->commit();
} catch (\Exception $e) {
$model->rollBack();
$msg = $e->getMessage();
ajax(-1, $msg);
}