是什么
事務是為了防止,多個操作,其中有失敗,數據有部分被執行成功的時候使用的。
比如,銀行,用戶轉賬。張三錢扣了,結果李四錢還增加!
這個時候需要使用事務,確保張三錢扣了,李四的錢也增加,才真正的成功!
能干嘛
確保數據的一致性!
如何使用呢?
/**
* 啟動事務
* @access public
* @return void
*/
public function startTrans() {
$this->commit();
$this->db->startTrans();
return ;
}
/**
* 提交事務
* @access public
* @return boolean
*/
public function commit() {
return $this->db->commit();
}
/**
* 事務回滾
* @access public
* @return boolean
*/
public function rollback() {
return $this->db->rollback();
}
使用任何的model對象都可以開啟。
// 開啟事務
$mgoodsModel->startTrans();
$errcount = 0;
...
if ($errcount == 0) {
// 提交事務
$mgoodsModel->commit();
$this->outData['code'] = 1;
$this->outData['msg'] = '添加成功';
$this->printOut();
} else {
// 事務回滾
$mgoodsModel->rollback();
$this->outData['code'] = 2;
$this->outData['msg'] = '添加失敗';
$this->printOut();
}
小結。人生在於折騰,編程在於折騰,工作在於折騰。折騰過的東西,才屬於你。
