本文实例讲述了thinkPHP事务操作。分享给大家供大家参考,具体如下:1.说明Db::startTrans();try
本文实例讲述了thinkPHP事务操作。分享给大家供大家参考,具体如下:
1.说明
Db::startTrans();
try{
操作;
对象->save();
Db::commit();
}
catch (Exception $ex) {
Db::rollback();
throw $ex;
}
2.案例
private function createOrderByTrans($snap)
{
Db::startTrans();
try {
$orderNo = $this->makeOrderNo();
$order = new OrderModel();
$order->user_id = $this->uid;
$order->order_no = $orderNo;
$order->total_price = $snap['orderPrice'];
$order->total_count = $snap['totalCount'];
$order->snap_img = $snap['snapImg'];
$order->snap_name = $snap['snapName'];
$order->snap_address = $snap['snapAddress'];
$order->snap_items = json_encode($snap['pStatus']);
$order->save();
$orderID = $order->id;
$create_time = $order->create_time;
foreach ($this->oProducts as &$p) {
$p['order_id'] = $orderID;
}
$orderProduct = new OrderProduct();
$orderProduct->saveAll($this->oProducts);
Db::commit();
return [
'order_no' => $orderNo,
'order_id' => $orderID,
'create_time' => $create_time
];
} catch (Exception $ex) {
Db::rollback();
throw $ex;
}
}
thinkPHP
事务操作